After reading Effective Java, I had an Epiphany

6 Avoid creating unnecessary objects

Literally, everyone knows that creating unnecessary objects is a bad idea. But the main purpose of this section is to remind us to avoid unintentional code writing that creates unnecessary objects.

Case 1:

String s = new String("abc");
Copy the code

The correct answer to this question is:

String s = "abc";
Copy the code

The reason is that the first method creates a new String instance each time it is executed, but these are all repetitions!


Example 2:

We use static factory methods in preference to constructors to avoid creating unnecessary objects, such as Boolean. ValueOf (String) is always used in preference to constructor Boolean(String). Because the constructor creates a new object each time it is called, static factories do not.


Example 3: When you create expensive objects, you should cache them.

Matches creates an instance of Pattern inside the String. This is expensive because regular expressions need to be compiled into finite state machines, so it should be cached:

public class RomanNumerals {
	private static final Pattern ID = Pattern.compile("^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$");
	
	static boolean isRomanNumeral(String s){
		returnID.matcher(s).matches(); }}Copy the code

In this way, the same ID instance is reused every time isRomanNumeral is called


Example 4: The above Pattern instances are invariant, but in certain scenarios instances are mutable, which is where adapters can be considered. An adapter is an object that delegates functionality to a fallback object, providing the fallback object with an interface that replaces previous functionality.

The KeySet method of the Map interface, for example, returns the same Set instance every time it is called. Although Set instances are mutable, when one of them changes, the others change because they are themselves one.


Example 5: Base types are preferred over boxing types because of the following example:

private static long sum(a){
	Long sum = 0L;
	for(long i = 0; i <= Integer.MAX_VALUE; i ++)
		sum += i;
		
	return sum;
}
Copy the code

This program executes without any problems, but it is slower because sum is of type Long rather than Long, so the program constructs about 2^31 Long instances.


This point in my memory and work requirements are not consistent, so I specially read the Alibaba Java development manual, which is described as follows:It can be seen that the company gives priority to the business in this issue, so friends can consider the choice when using. I personally recommend using the packaging type.

One mistake to avoid: don’t get caught up in the logic that creating objects is very expensive after this chapter, instead it’s a mistake to maintain your own object pool to avoid creating objects. Because modern JVM implementations have highly optimized garbage collectors, performance can easily exceed that of lightweight object pools.

A good example is database connection pooling, because establishing a connection to a database is very expensive.