View RSS Feed

Advanced Java

Advanced Java programming.

  1. Unnecessary Object Creation with Immutable Objects

    by , 11-29-2011 at 11:05 PM
    Programmers are often not aware of how to create objects that can be reused. The key to understanding how to create reusable objects is that they must be immutable. So for example if you a running a loop in which each loop a string instance is required, rather than create a new instance every time it executes, you simply reuse the existing instance.

    Rather than create a new String instance.

    Java Code:
    public void BuildingLoop{
    	int total = 10;
    	
    	for(int i=0;
    ...
  2. Unnecessary Object Creation with Mutable Objects

    by , 11-29-2011 at 10:59 PM
    Continuing with our last tip on immutable objects, we now look at re-using mutable objects. Although you cannot modify a mutable object, you can avoid unnecessarily waste memory by reusing the existing instance. So consider for example mutable Date objects. Once the values are computed, they are never modified. So for example if you a running a loop in which each loop a string instance is required, rather than create a new instance every time it executes, you simply reuse the existing instance. ...