Minimizing the scope of local variables
1. Where the variable is used for the first time declaration
2. Declarations of almost every variable should contain an initialization expression: The Try-catch statement is an exception
3, make the method small and focus is a good strategy
Second, the For-each cycle takes precedence over the traditional for loop
1, For-each cycle in the simplicity and prevention of bugs have a traditional for loop incomparable advantage, and no performance loss.
2. Three types of cases where the For-each cycle cannot be used:
A, filtering: The Remove method that requires an explicit iterator
B, Conversion: an explicit iterator or array index is required to set the value
C, parallel iterations: Multiple related sets are moved synchronously.
Iii. understanding and using class libraries
1, using the class library is standing on the shoulders of giants
Iv. If precise answers are required, avoid using float and double
1, float and double types are designed for fast calculation and are not accurate. In particular, it is not possible to use BigDecimal, int or long instead of currency calculations.
2, int can be 9 digits, long can be 18 digits, other must use BigDecimal type.
The basic type takes precedence over the boxed basic type.
1. The three main differences between the basic type and the boxed base type:
A, basic type only value
b, the basic type only has the function complete value, the boxing type also has the non-function value: null.
C, the basic type saves time and space.
2. The difference between the base type and the boxed type cannot be ignored, especially when dealing with various operators.
If other types are more appropriate, try to avoid using strings
1, the string is not suitable for other value types: Read input should be completed as soon as possible after the conversion work.
2. String not suitable for enumeration type
3, the string is not suitable to replace the ability table
Vii. Beware of string connection performance
1, because of the non-variability of the string class, the efficiency of the connection operator is not high. You can use the StringBuilder class or a character array.
Viii. referencing objects through interfaces
1. Use interfaces rather than classes to refer to objects.
2. If no interface is available, consider the abstract base class.
Ix. interface takes precedence over reflection mechanism
1. Defect of reflection mechanism
A, loss of the benefits of compile-time type checking
b, the code needed to perform the reflection access is very clumsy and lengthy.
C, Performance loss
2, usually only the class must work with the class that is unknown at compile time to need reflection. Used only to instantiate objects, it is best to use interfaces or super-classes to access objects.
Ten, using local methods with caution
1. Three main uses of local methods
A, the ability to "access platform-specific mechanisms" is provided
B. Ability to access legacy code repositories
C, improve system performance
2. As the Java platform matures, fewer local methods are needed.
Xi. careful optimization
1. Do not optimize
2, to work hard to write a well-structured program rather than a fast program.
3, rely on profiling tools to assess the performance of the program, any prediction is unreliable.
12. Adherence to universally accepted naming conventions
Effective Java Reading notes of the seven general programming