Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wrapper classes

 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh! such a simple solution i now understand it properly
thanks ;)
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but as you know if you created the Integer like the below code, you will get a result for the 10 like for 1000 :
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideally when you write this code, Line 1 and 2 should be read as
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic