Items in one sentence about java
- Always assume most extreme inputs and most weird operations.
- Assume user’ hardware configuration(CPU/memory/disk) is lower than average level.
- Use BufferedInputStream and BufferedOutputStream for file I/O.
- Use try-with-resource for file I/O.
- Always consider out of memory when read/write file or data.
- Notice charset when read/write text file.
- Notice charset when convert between bytes and string.
- Condition to determine whether reach end of file should be "len > 0". If use "len != -1", endless loop may happen if zero byte is read.
- Call "System.gc()" before delete/move/rename file, to avoid failure due to cache.
- When handle information in tree, like files system, endless loop should be avoided. If the operation will change source node, the target node should not be the source node nor its descendants.
- If delete list by indices, delete items from the end index to the start index.
- When class has many parameters, do not write many constructors to initialized different sets of parameters. Better coding is static creator and chain assignment by return “this” in all “set” methods.
- Anonymous class is very useful for scenario like lots of duplicated codes with only difference that several methods have difference implements.
- Generic is very useful for scenario like lots of duplicated codes with only differernce of data type.
- Define as less instance variables/methods as possible in class if it generates lots of instance.
- As less float-point calculations as possible in loop. If can, change float-point calculations as integer calculations.
- When listen data changing, need judge whether change trully happens, and avoid too frequently manufactures by setting minimum threshold of changing.
- Reboot application when change JVM parameters or need clean env in simple way.
- Notice whether the index is 0-based or 1-based, especially for thrid-party codes. 0-based is default. If 1-based, should write comment for it.
- Generally, index in user interface starts from 1, while index in codes data starts from 0.
- Generally, range(start-end) in user interface includes end, while range(start-end) in codes data excludes end.
- Double.MIN_VALUE is the minimum positive double(almost zero). -Double.MAX_VALUE is the minimum double.
- When handle money, store integer numbers for each unit.
- BigDecimal is much slower than double. Double is enough for most situations.
- To determine value's broken, write if (abs(x - 7.3) < TOLERANCE) instead of if (x == 7.3).
- Value of final static constant should be determined at compling time.
- When JDBC read numbers, if SQL value is null, ResultSet.getDouble/getFloat/getInt/getShort returns 0. Use ResultSet.getObject instead if need distinct zero and null. ResultSet.html#getDouble
- From Java18, Charset.defaultCharset() is always UTF-8. This will affect commands and results of ProcessBuilder. Process JDK-8266075