aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing doubt" Watch "Boxing doubt" New topic
Author

Boxing doubt

Hardik Raja
Ranch Hand

Joined: Feb 07, 2006
Posts: 117
In the below Code Integer Wrapper Object is created in 2 different ways and some condition is evaluated based on == and equals() method..

why is the behaviour different in the both the cases....??

CASE 1 : EXECUTE THE BELOW CODE WITHOUT ANY MODIFICATION

CASE 2 : COMMENT THIS

AND UNCOMMENT THIS
AND THEN EXCEUTE

AFTER SEEING THE OUTPUT IN BOTH CASE...PLEASE GIVE YOUR SUGGESTIONS ON WHY THE OUTPUT IS DIFFERENT IN BOTH CASES




One more thing...
What is the difference between ??

Integer i1 = new Integer(10)
and
Integer i1 = 10;
[ April 10, 2007: Message edited by: Hardik Raja ]
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
In order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same:
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127

When == is used to compare a primitive to a wrapper object, the wrapper object will be unwrapped to its primitive value, and the comparison will be made between the two primitives' values.

This condition holds false when the objects are created using the new operator. In that case, the new operator will create a new object & hence the references pointing to them will differ.

So in your case, when you say :


What really happens is that only one Integer object is created in the pool & both the references point to that same object. But when you say :

Here, 2 new objects are created & i1 & i2 point to 2 different objects & hence the output is "DIFFERENT OBJECTS".

Hope this helps.

Regards,
Sourin.


Regards,<br />Sourin.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Raja,

Behavior of equals() has universal so far as Wrappers are concerned because each wrapper implements the equals method of the Object class obviously.
No matter in what way you create the wrapper object, equals will always return true if values are same.

Question only arises in case of ==.

You know the rules that says in the range of byte if two wrappers created without using "new" operator will result true. (I mean using autoboxing technique)

It is all about what Java language designers thought to save memory in case value is within -128 to 127.

Regards,
cmbhatt


cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boxing doubt
 
Similar Threads
Help me with this Wrapper class code
keyword "new"
how is this possible please explain
Wrapper comparison.
how is this possible please explain