This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wrapper classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wrapper classes" Watch "wrapper classes" New topic
Author

wrapper classes

amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46



output:really equal
not equal


when i use i1=10 and i2=10
output: equal
really equal

when am using 1000 as the value of i1 and i2, the == test fails but for 10,it pass. Can someone explain me this unexplained behaviour? am really confused

Reference: k&b pg-246


ocpjp 6.0 certified.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

The JVM uses a cache for integer wrappers in the range -128 to 127. So when you create two wrapper objects in that range using autoboxing (or calling the valueOf method), then the object from the cache is used. Thus if you create two Integer with the same value in that range, both will be the same object in the heap thus the == comparison will return true...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

Actually there are some rules for immutability applied to the Wrapper classes
• Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same

hence
it gives true for value 10 and false for the value 1000

hope this helps
happy preparation

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

ohh! such a simple solution i now understand it properly
thanks ;)
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
but as you know if you created the Integer like the below code, you will get a result for the 10 like for 1000 :


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181


Is there exist a separate pool for all the immutable instances on the heap? Because the behavior shown by Wrapper class instances here is also shown by the String literals as well.
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
I get this from K&B Page 246
{ In order to save memory, two instances of the
following wrapper objects (created through boxing), 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
Note: When == is used to compare a primitive to a wrapper, the wrapper will be
unwrapped and the comparison will be primitive to primitive.}
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Ideally when you write this code, Line 1 and 2 should be read as
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

even though Integer and int are same??

What made you jump to that conclusion? int is a primitive, Integer is a reference type. How are they the same??
 
wood burning stoves
 
subject: wrapper classes
 
Similar Threads
Doubt in equals and == . Please help.
Doubt in Wrapper
Can someone help me with this
Help me with this Wrapper class code
Query with == for Boolean