Java Standards
1. Prohibited Items
(1) Do not use System.out.println, System.err.println, etc. for logging.
/* You must use Logger for logging */
System.out.println("#Customer ID:" + input.getId());
System.err.println("#Array Count:" + input.getCount());
(2) Do not create DB Connection directly.
Connection conn = null;
try {
/* Do not create Connection directly in source code */
conn = DriverManager.getConnection("jdbc:oracle:thin@127.0.0.1:orcl");
} catch (Exception e) {
e.printStackTrace();
}
(3) Remove unused member variables, local variables, and methods.
2. Notes on Using Immutable Objects
An immutable object is an object whose value does not change after creation. Therefore, immutable objects do not have set methods and their member variables cannot be changed.
2.1. java.math.BigDecimal
For items that require precise value calculations such as amounts and interest rate calculations, do not use primitive types such as int, long, float, double (including Wrapper Classes), but use the java.math.BigDecimal class. Notes for use are as follows.
(1) Methods related to operations such as add, subtract, multiply, divide, round, abs, etc. must always assign the value to the left-hand side.
BigDecimal bdOne = new BigDecimal(10);
BigDecimal bdTwo = new BigDecimal(20);
/* Incorrect example */
bdOne.add(bdTwo);
/* Correct example */
bdOne = bdOne.add(bdTwo);
(2) For value comparison, do not use the '==' operator between objects, but use the compareTo method.
BigDecimal bdOne = new BigDecimal(10);
BigDecimal bdTwo = new BigDecimal(20);
/* Incorrect example */
if(bdOne == bdTwo) {
... ...
}
/* Correct example */
int result = bdOne.compareTo(bdTwo);
if(result == 0) {
... ...
}
(3) For detailed usage of the BigDecimal class, refer to the Java API.
2.2. java.lang.String
Like BigDecimal objects, String objects are also immutable objects.
(1) When using methods related to operations such as concat, replace, etc., you must always assign the value to the left-hand side.
String strOne = "ABC";
/* Incorrect example */
strOne.replace("A", "Z");
/* Correct example */
strOne = strOne.replace("A", "Z");
(2) For comparison of String values, do not use the '==' operator, but use the equals method.
/* Incorrect example */
if("CODE00" == code) {
... ...
}
/* Correct example */
if("CODE00".equals(code)) {
... ...
}
(3) For string + string operations, use the StringBuilder class.
String[] userNameArr = new String[]{"Peter", "Steve", "Sally"};
/* Incorrect example */
String msg1 = null;
for(String name : userNameArr) {
msg1 += name + ",";
}
/* Correct example */
StringBuilder msg2 = new StringBuilder();
for(String name : userNameArr) {
msg2.append(name).append(",");
}
(4) For detailed usage of the String class, refer to the Java API.