• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boxing doubt

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic