| Author |
Can someone help me with this
|
Neeta Avasthi
Greenhorn
Joined: Sep 19, 2007
Posts: 1
|
|
Hello, I am new to the group of ranchers...just wanted to clarify a doubt: This code is there in the K & B book...chapter 3. The output for the program is : same object meaningfully equal different objects meaningfully equal I am really confused as to how both Line A and B can be executed. class Test { public static void main(String agrs[]) { Integer i3 = 100; Integer i4 = 100; if(i3 == i4) System.out.println("same object");//line A if(i3.equals(i4)) System.out.println("meaningfully equal"); Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println("different objects");//line B if(i1.equals(i2)) System.out.println("meaningfully equal"); } } Thanks in Advance.
|
 |
Krishna Srinivasan
Ranch Hand
Joined: Jul 28, 2003
Posts: 1803
|
|
Normally, when the primitive types are boxed into the wrapper types, the JVM allocates memory and creates a new object. But for some special cases, the JVM reuses the same object. The following is the list of primitives stored as immutable objects: boolean values true and false All byte values short values between -128 and 127 int values between -128 and 127 char in the range \u0000 to \u007F read this article for detailed explanation. [ January 09, 2008: Message edited by: Krishna Srinivasan ]
|
Krishna Srinivasan
OCAJP Mock Questions
|
 |
Dean Jones
Ranch Hand
Joined: Dec 29, 2007
Posts: 129
|
|
prints false prints true; prints false
|
 |
 |
|
|
subject: Can someone help me with this
|
|
|