• 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

Wrapper Classes Autoboxing Concept

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output :

Different Objects
Meaningfully Equivalent Objects
Different Objects
Same Objects


Please Explain !!! < I think it is flaw in JAVA >

Take care
Cheers!!!
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Integer i3 = Integer.valueOf(12);
Integer i4 = new Integer("12");



i4 is created using new operator and so when you compare it with other wrappers they are not same(meaning they are different)

with new operator you are constructing a new object so with new opeartor you will be creating new objects which will not be the same even if those 2 objects are constructed to hold same primitive values.. Do trial and error method while coding;; you will get to know..

but i5 and i6 are same as (check K&B for assistance)

i am just a learner ;; i hope you get a better view of this concept from experts;;;

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the explanation from K&B book (Chapter#3)

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




Output:
true : true
false : true
reply
    Bookmark Topic Watch Topic
  • New Topic