Effective Java 3/E summary chanpter 8 (Shorter version)
29 Mar 2021Chanpter 8 : General Programming
You can find the longer version of this summary here.
- Minimize the scope of local variables
- Declare a varaible when it is first used
- Keep methods small and do only one function
- Prefer for-each loops to traditional for loops
- Avoid using for-each loops for Destructive filtering, transfroming, parallel iteration
- Know and use the libraries
- Avoid float and double if exact answers are required
- Float and double are designed for scientific and engineering calculations. (Follows
IEEE 754floating point) - Use
BigDecimal,intorlong!
- Float and double are designed for scientific and engineering calculations. (Follows
- Prefer primitive types to boxed primitives
- Null safe
- Use
== - Performance
- Avoid strings where other types are more appropriate
- If you use
Stringas a key, it is used globally and is vulnerable to security.
- If you use
- Beware the performance of string concatenation
- String concatenation is
O(n^2)
- String concatenation is
- Refer to objects by their interfaces
- Declare interface type as possible for params, return, var, field
- Prefer interfaces to reflection
- Reflection: Java code exists in static area as byte code, and constructor, method, and field can be accessed only with the name of the class.
- No type checking
- Code smell
- 11 times slower
- Use
reflectiononly when creating it, and refer to it by aninterfaceor ahigher class.
- Reflection: Java code exists in static area as byte code, and constructor, method, and field can be accessed only with the name of the class.
- Avoid using JNI (Java Native Interface)
- Don’t optimize, do it when you need it through profiling.