• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Wrapper Doubt 2

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is an abstract from K&B book page 236...Chap3...Assignments

Yikes! The equals() method seems to be working, but what happened with ==
and != ? Why is != telling us that i1 and i2 are different objects, when == is saying
that i3 and i4 are the same object? In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
n Boolean
n Byte
n Character from \u0000 to \u007f (7f is 127 in decimal)
n Short and Integer from -128 to 127



The above concept also applies to Long from -128 to 127...
i.e Long ln = 1L;
Long ln2 = 1L;

(ln==ln2) returns true

...then why its not included in the book..is it an errata...?
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hardik,

I couldn't get your question.

Please provide the complete question.

Gitesh
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hardik Raja:
...The above concept also applies to Long from -128 to 127...


Maybe.

JLS - 5.1.7 - Boxing Conversions says...

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.


Note that type long is not included in this statement.

This does not mean that long values will not behave in this manner. As you've demonstrated, long values actually do behave this way on your platform. But this behavior is not guaranteed.

(Note that it's also not specified what will happen for values outside of this range. The wrapper objects are not required to be equal, but they're not required to be unequal either. The only behavior we can be certain of is what's covered by the JLS statement above.)
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic