Unnecessary Object Creation with Immutable Objects
by , 11-29-2011 at 11:05 PM (1338 Views)
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; i< total; i++){ String str = new String("XXXYYYZZZ-AAABBBCCC" + String.valueOf(i)); ..... } } You need to reuse the existing instance. public void BuildingLoop{ int total = 10; for(int i=0; i< total; i++){ String str = "XXXYYYZZZ-AAABBBCCC" + String.valueOf(i); ..... } }









Email Blog Entry
Size Reduced for Images in PDF &...
05-15-2013, 05:53 PM in Java Software