*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can someone help me with this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can someone help me with this" Watch "Can someone help me with this" New topic
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: 1844

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
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129

prints false
prints true;
prints false
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone help me with this