New Expression Optimization

Java supports automatic garbage collection. What this means is all objects allocated using the new expression are deallocated automatically by the garbage collection. However, where an object allocated using new is never used, it is much more optimal to just remove the call to new. Java guarantees initialization of all allocated objects. By removing the call to new, in addition to the default initialization, significant gain in speed can be achieved.


In the code fragement below, the object a is not used and hence the new expression can be deleted.

	int a[];
	a = new int[100];

Below is the code fragment after New Expression Optimization.

	// a not used and hence not allocated/initialized


New Expression Optimization is a very important optimization.

Some programs may not need default initialization of objects. Detecting such cases and avoiding the default initialization will provide significant performance gain when large arrays are involved.