• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

wrapper classes

 
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh! such a simple solution i now understand it properly
thanks ;)
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.}
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ideally when you write this code, Line 1 and 2 should be read as
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic