• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Wrapper Classes Autoboxing Concept  RSS feed

 
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!!!
 
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;;;

 
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
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!