• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Integer's == and !=

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
MyExamCloud Software Support
Posts: 151
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic