Write high-quality Java code

Java development general methods and guidelines

 

Do not use confusing letters in variables and constants: int i=1l ;

Don't make constants into variables;

The value type of the ternary operator must be consistent;

 

Avoid method overloading with variable-length parameters: Java 5 introduced variable-length parameters, varags, represented by ... , the variable-length parameter must be the last parameter, and a method cannot have multiple variable-length parameters;

Don't let null and null values ​​affect variable-length methods, mainly in the overloading of variable-length methods;

 

Less use of static imports: such as excessive use of import static XXX , the maintainability is reduced

Do not overwrite statically imported variables and methods in this class. Be careful of the compiler's shortest path principle: variables, constants, and methods that can be found in this class will not be found in other packages or parent classes. Properties and methods take precedence.

 

Avoid assigning final constants in the constructor of serialized classes

 

Volatile business written in scripting language: can be released at any time without redeployment

 

basic type

Use even numbers to judge, not odd numbers: use i%2==0 instead of i%2==1 , because i=-1 will be judged as an even number

Use integer type to process currency: first expand N times processing operation, divide N times when displaying

Pay attention to the boundary: such as int n=1000, int input+n<2000, when the input input exceeds the maximum value of Int , the input+n is a negative number and still meets the conditions

Don't let rounding be a loss, avoid it with the RoundingMode class, which provides the Round mode

== in Java is to judge whether the two operands are equal, if it is an object, then judge whether the reference is the same

 

class, object, method

There should be no implementation code in the interface;

Static variables must be declared before assignment

Do not overwrite static methods

 

The constructor should be as simple as possible, not throwing exceptions as much as possible, and not doing complex algorithms

Avoid initializing other classes in the constructor

 

Use the construction code block to refine the program: there is no prefix, just pick it up with {} , the compiler will insert the construction code block directly into the front end of the constructor; but when the this keyword is encountered, the constructor will not be inserted.

 

Use static inner classes to improve encapsulation

Making Multiple Inheritance a Reality: Implementing with Inner Classes

 

Make the utility class uninstantiable: in addition to setting the constructor to private , throw an exception

 

Copying objects using serialization

 

Overriding equals must implement the HashCOde method

 

It is recommended to override the tostring method

 

string

String对象直接赋值,不用new

Java为了避免在一个系统中产生大量String对象,设计了string池。创建机制:创建一个字符串时,先检查池中是否有字面值相等的字符串,有的话则指向该引用;无的话才创建。

Intern方法会检查当前对象在字符串对象池中是否有字面值相等的字符串,有的话则指向该引用;无的话才放在池中。

 

准确适用StringStringBufferStringBuilder

String是不可改变的量,创建后不再修改;

StringBuffer是一个可变字符串,StringBufferappendString的“+”的区别的,+之后对应的引用变了,StringBuffer的引用不变值在变。

StringBuilder是线程不安全的 StringBuffer是线程安全的

 

建议使用UTF8编码

 

中文字符串排序最好指定Collector排序

Comparator c = Collector.getInstance(Local.CHINA);

Arrays.sort(sre,c)

 

异常

提倡异常封装:尽量try catch 不抛出

采用异常链传递异常

不要再finally中处理返回值

 

多线程合并发

不推荐覆写start方法

不用过时的stop方法停止线程

 

性能和效率

不在循环条件中计算

除非必要,不要克隆对象

调整JVM参数

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326682297&siteId=291194637