wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer's == and != Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer Watch "Integer New topic
Author

Integer's == and !=

Karthika LTemp
Greenhorn

Joined: Jul 15, 2006
Posts: 1
Could anyone execute the following set of instructions and explain the behaviour

Integer i1 = 1001;
Integer i2 = 1001;

Integer i3 = 24;
Integer i4 = 24;

if(i1 == i2) System.out.println("i1==i2");
if(i1 != i2) System.out.println("i1!=i2");
if(i1.equals(i2)) System.out.println("i1 equals i2");

if(i3 == i4) System.out.println("i3==i4");
if(i3 != i4) System.out.println("i3!=i4");
if(i3.equals(i4)) System.out.println("i3 equals i4");


Thanks
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Why does the autoboxing conversion sometimes return the same reference?

What are the differences between the '==' operator and the equals() method?
[ October 07, 2006: Message edited by: wise owen ]
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
Originally posted by Karthika LTemp:
Could anyone execute the following set of instructions and explain the behaviour

Integer i1 = 1001;
Integer i2 = 1001;

Integer i3 = 24;
Integer i4 = 24;

if(i1 == i2) System.out.println("i1==i2");
if(i1 != i2) System.out.println("i1!=i2");
if(i1.equals(i2)) System.out.println("i1 equals i2");

if(i3 == i4) System.out.println("i3==i4");
if(i3 != i4) System.out.println("i3!=i4");
if(i3.equals(i4)) System.out.println("i3 equals i4");


Thanks



I had the same question a couple of days ago. Now I solved it. I think the output will be like this (I didn't run through JVM though, point me out if I am wrong):
i1!=i2
i1 equals i2
i3==i4
i3 equals i4

the trick is that autoboxing gives you same object if the date range is from -128 to 127 any integer type wrapper classes.
rajeswari kannan
author
Ranch Hand

Joined: Oct 01, 2006
Posts: 80
The code demonstrates the usage of autoboxing and unboxing feature and the equality of two objects or two primitives.

Autobxing & unboxing is the new feature introduced in Java Tiger to automatically convert primitives into respective wrapper object/object to primitive type.

The primitives are equal and the values of the boxed ints are equal in the case of small integral values [-128, 127], . In this case the objects are cached in a pool much like Strings. When i3 and i4 are 24, a single object is referenced from two different locations.

But i1 and i2 are 1001, two separate objects are referenced.
Autoboxing is guaranteed to return the same object for integral values in the range [-128, 127], but an implementation may, at its discretion, cache values outside of that range.

Note: You need JDK 1.5 or higher to run the code.

Hence the code flow is..

Integer i1 = 1001; // 1001 int primitive autoboxed to Integer object i1
Integer i2 = 1001; // 1001 int primitive autoboxed to Integer object i2


Integer i3 = 24; // 24 int primitive autoboxed to Integer object i3
Integer i4 = 24; // 24 int primitive autoboxed to Integer object i4


if(i1 == i2) System.out.println("i1==i2"); // object i1 compared with object i2 by == returns false

if(i1 != i2) System.out.println("i1!=i2"); // object i1 compared with object i2 by == returns false

if(i1.equals(i2)) System.out.println("i1 equals i2"); // object i1 compared with object i2 by Object.equals(Object) returns true


if(i3 == i4) System.out.println("i3==i4"); // returns true (int within range [-128, 127] )
if(i3 != i4) System.out.println("i3!=i4"); // returns false
if(i3.equals(i4)) System.out.println("i3 equals i4"); // returns true
[ October 07, 2006: Message edited by: rajeswari kannan ]

Rajeswari Kannan Java Certification | Online Exam Software | OCAJP | OCPJP | OCEJWSD | OCEEJBD
OCEJPAD | SCJD | SCWCD | SCBCD | SCDJWS | OCMJEA | OCEJWCD | SCMAD
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
As James predicted, the code returns:



if(i1 == i2) System.out.println("i1==i2"); // object i1 compared with object i2 by == returns false

if(i1 != i2) System.out.println("i1!=i2"); // object i1 compared with object i2 by == returns false

if(i1.equals(i2)) System.out.println("i1 equals i2"); // object i1 compared with object i2 by Object.equals(Object) returns true


Referring to the above code snippet, if i1 == i2 is false, then i1 != i2 returns true, not false.

Also, it is not Object.equals(Object) being used here it is Integer's overridden version Integer.equals(Object).
[ October 08, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer's == and !=
 
Similar Threads
Small doubt in Boxing & == & equals()
how is this possible please explain
KB 235
OO basics doubt
Boxing, == and equals