Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Classes Autoboxing Concept

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • 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!!!
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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;;;

 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic