• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Confusion in Integer Object

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Integer i1 = 125;
Integer i2 = new Integer(125);
System.out.println(i1==i2);-->false


Why it is giving "false". Its in range of -127 to 127.

Whats the difference between these two ways of creating integer.

whats the meaning of int i = new Integer(5);

Please explain me this Integer funda in details.
I am always doing mistake in this type (== operator) question.

Please help me.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer is not int

int is a primitive
Integer is an object

i2 is a reference to an Integer object containing the value of 125. i2 is not the value itself.

When you see the following:

Object variable = new Object();

remember that the variable is not the value. It's more like a remote control that points to the object.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am always doing mistake in this type (== operator) question.



Now you wont do mistake in that, i hope.

As you already know about the range 127 to -128, but just know one more
thing that this applies when both references are referring to objects created
with autoboxing instead of with new operator.




whats the meaning of int i = new Integer(5);



It is new feature with Java 5.0 know as auto unboxing.
Integer object is automatically unboxed to primitive int.


Thanks,
[ May 24, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you both kinda right guys (Gunjan , Alan). when you create an Integer in range -128 to 127
with boxing it is guaranted to box to the same object. However you didnt create the i2 Integer object with Boxing you constructed it, remember whenever you create an object with 'new' a new object will be made on the heap. i3 and i4 which i added box to the same object, hope this helps

Integer i1 = 125;
Integer i2 = new Integer(125);
System.out.println(i1==i2); //false

Integer i3 = 125;
Integer i4 = 125;
System.out.println(i3==i4); //true
[ May 24, 2007: Message edited by: Louis Moloney ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


question:
In (i == ii) (where the value is in the range -128 - +127), is the jvm doing an autoboxing of i before comparison or auto-unboxing of ii before comparison? I assume the second one as autoboxing will result in two different object.
[ May 24, 2007: Message edited by: M Krishnan ]
 
Louis Moloney
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 120;
Integer ii = new Integer(120);
Integer iii = 120;

System.out.println( i == ii); //true

when comparing a primitive to a wrapper object , java unboxes the wrapper to be a primitive. the comparison is done on 2 primitives
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All possible combo:


In summary:
comparison using (==) operator:

Primitive vs Wrapper ( with same value) --> true, always

Two constructed wrappers (with same value ) -->
true, for values in the range -128 to + 127
false, for all other values

Two instantiated wrappers (with same values) --> false , always

(
where
'constructed wrapper' eg. Integer i = 1000;
'Instantiated wrapper' eg. Integer i = new Integer(1000);
)
[ May 24, 2007: Message edited by: M Krishnan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic