Initializing wrapper objects
Posted: Fri Oct 09, 2020 4:37 pm
Hi all,
i have a question about initializing wrapper objects. According to the book this can be done in three ways:
1. by a constructor
2. valueOf() method
3. Autoboxing
If i understand correct:
The first way always makes a new wrapper object and the second and third may make a new wrapper object if its not already available on the heap.
so why does this comparison return false? in my perspective in the code below is first a new Integer object created on the heap and then on the second line i1 should be a cached object and refer to the same location in memory. which should result in a true?
tnx in advance!
i have a question about initializing wrapper objects. According to the book this can be done in three ways:
1. by a constructor
2. valueOf() method
3. Autoboxing
If i understand correct:
The first way always makes a new wrapper object and the second and third may make a new wrapper object if its not already available on the heap.
so why does this comparison return false? in my perspective in the code below is first a new Integer object created on the heap and then on the second line i1 should be a cached object and refer to the same location in memory. which should result in a true?
Code: Select all
Integer i2 = new Integer(10);
Integer i1 = 10;
System.out.println(i2==i1);