wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion in Integer Object 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 "Confusion in Integer Object" Watch "Confusion in Integer Object" New topic
Author

Confusion in Integer Object

Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
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.


Gunjan Kumar
SCJP 1.5
Alan Smithee
Greenhorn

Joined: Mar 21, 2006
Posts: 23
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.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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 ]

cmbhatt
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
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 ]

Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178


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

Joined: Feb 06, 2007
Posts: 59
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

Joined: Aug 13, 2006
Posts: 178
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion in Integer Object