Garbage Collector

If you have an object of Wrapper class inside any class, then the wrapper classes maintain their immutability upto a max of 127!! If the value of the instances of wrapper exceeds 127, then the immutability is lost and the instances are duplicated thereby becoming eligible for garbage collection when the actual instance of the outer class is being set to null!

Look at the example:

class CardBoard {
   Short story = 5;
   CardBoard go(CardBoard cb) {
      cb = null;
      return cb;
   public static void main(String[] args) {
    CardBoard c1 = new CardBoard();
    CardBoard c2 = new CardBoard();
    CardBoard c3 = c1.go(c2);
    c1 = null;
   // do Stuff

Here the question may be asking about how many objects would be eligible for garbage collection?

We may just think of the instances being referred by (pointed by) the reference variables of the outer class CardBoard namely c1,c2 and c3.

Actual answer for this thread may be: only one object referred by c1; (c3 just gets a null reference as an initial value since the go() method returns null; moreover there is no new object created at all for c3!).

Actually there is a wrapper class instance Short story = 5; in the class CardBoard!

It holds good as an immutable object until its value reaches 127. Till then, its immutable and all the objects of the outer class CardBoard share the same Short object.Once its value reaches beyond 127, then all objects might have their own copies of story instance in turn.

So, as far as the value of story remains <127, the answer is ONE object only (as if the Short object does not exist). If it has exceeded 127, then there would be two objects as the Short object being pointed by the reference variable c1 also eligible for gc; where as the object still not eligible for gc "c2" has its own copy of "story" instance inside it.

The concept taken from the discussion on the JavaRanch URL:

Its possible only for JDK 1.5 i suppose. I have tested with a sample program in JDK 1.4. The immutability as described above does NOT hold good!

Got it confirmed in the same thread of JavaRanch. Its because, only in Java 5.0, autoboxing/unboxing features are introduced and with which the Wrapper classes's immutability feature got changed with the upper bound of 127, means if the value exceeds 127, the immutability is lost! ;-)

Sample Program:
Look at the sample program below for the JDK 1.4 version for not having the mutable property even if the value inside the wrapper object exceeds 127.

class MyOwnClass{    
    Byte byteWrapper1 = new Byte((byte)128); // >127!!    
    Byte byteWrapper2 = new Byte((byte)125); // <127    
    byte bytePrimitive = 127;    
    String stringVar = "WrapperImmutableTest";     
    public String toString()    {        
        return ("byteWrapper1 = "+this.byteWrapper1+", byteWrapper2 = "+this.byteWrapper2
            +", bytePrimitive = "+this.bytePrimitive+", 
            stringVar= "+this.stringVar);    
public class WrapperClassImmutabilityTest {     
    public static void main(String[] args) {     
    MyOwnClass obj1 = new MyOwnClass();        
    MyOwnClass obj2 = new MyOwnClass();    
    System.out.println("\n ----- Initial Values ------\n");        
    System.out.println("obj1 has (1) -> "+obj1);                
    System.out.println("obj2 has (1) -> "+obj2); 
    /* Changing values of obj1 */        
    obj1.byteWrapper1 = new Byte((byte)12);        
    obj1.byteWrapper2 = new Byte((byte)120);        
    obj1.stringVar+=" - 2";         
    System.out.println("\n ----- Effects after changing obj1's value -----\n");
    System.out.println("obj1 has (2) -> "+obj1);                
    System.out.println("obj2 has (2) -> "+obj2);                

The output it provides is as follows:

obj1 has (1) -> byteWrapper1 = -128, byteWrapper2 = 125, bytePrimitive = 127, stringVar = WrapperImmutableTest
obj2 has (1) -> byteWrapper1 = -128, byteWrapper2 = 125, bytePrimitive = 127, stringVar = WrapperImmutableTest
----- Effects after changing obj1's value -----
obj1 has (2) -> byteWrapper1 = 12, byteWrapper2 = 120, bytePrimitive = 122, stringVar = WrapperImmutableTest - 2
obj2 has (2) -> byteWrapper1 = -128, byteWrapper2 = 125, bytePrimitive = 127, stringVar = WrapperImmutableTest

Hope the sample program was justifying the concept!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.