Wechat official account: Tangerine Java Technology pit Internet technology exchange & internal push group: 693961584 Article first nugget platform, follow-up synchronized update public account, reply “add group” after attention can join the Internet technology exchange & internal push wechat group, and discuss interview questions with a group of big factory executives. Reply “666” to obtain all information packs available on the First-line Internet (including development software, development specifications, classic e-PDFs, and some premium learning courses).
preface
Reread Effective Java – The beginning
1.1 Consider replacing constructors with static factory methods
Both static factory methods and constructors can create object instances, the difference being that the former is more explicit.
Public BigInteger(int,int,Random) returns a prime number. It is clearer to the user if the static method named BigInter. ProbablePrime is used
Static factory methods make the code more concise when creating parameterized instances.
Map<String,List<String>> map = new HashMap<String,List<String>>();
Copy the code
This can become verbose if there are more and more generic type parameters. But with static factory methods, the compiler can do type derivation, assuming that HashMap provides such a static method:
public static <K,V> HashMap<K,V> newInstance(a) {
return new HashMap<K,V>();
}
Copy the code
So you can use it like this when you use it
Map<String,List<String>> map = HashMap.newInstance();
Copy the code
Static factory methods are essentially the same as normal static methods. In our specifications, static factory methods are commonly named as follows: valueOf getInstance newInstance of newType, etc.
1.2 When multiple constructor parameters are encountered, the builder is preferred
A common limitation of static factories and constructors is that they do not extend a large number of optional parameters well.
Consider using the Javabeans pattern, where a no-argument construct is called to generate an object and the property is assigned through setter methods, but this pattern is divided into several calls during construction and does not guarantee the consistency of the object by verifying the validity of the constructor parameters.
Consider using the Builder pattern, which satisfies the readability of the Javabeans pattern, but in order to create an object, you must first create its builder, an overhead that might not be obvious in ordinary practice.
1.3 The ability to enforce non-instantiation through private constructors
Some, such as utility classes, do not want to be instantiated, and can use private constructors to avoid instantiation:
public class JsongUtil{
private JsongUtil(a){}}Copy the code
1.4 Eliminating expired object references
It is common to operate on the stack, and the operation out of the stack does not mean to release the object.
public Object pop(a){
if(size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null;
return result;
}
Copy the code
1.5 Avoid creating unnecessary objects
You can reuse objects and try to reuse objects without having to create an object that does the same thing. The created object is immutable, so it will be reused.
String str = new String("Orange pine Java");
Copy the code
This statement creates a new String instance each time it is executed, and if this is used in a loop or invoked frequently it will create many unnecessary instances. Can be rewritten as follows:
String str = "Orange pine Java";
Copy the code
This way there is only one instance, and the objects are guaranteed to be reused in the same JVM.
The last
- Articles are original, original is not easy, thanks to the mining platform, feel fruitful, help three even ha, thank you
- Wechat search public number: Orange pine Java technology nest, make a friend, enter the Internet technology exchange group
- All the code, sequence diagram and architecture diagram involved in the article are shared and can be requested for free through the public number plus group
- Article if there is a mistake, welcome to comment messages pointed out, also welcome to reprint, the trouble to mark the source is good