• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

100 == 100 but 1000 != 1000 - what's going on? [boxing, ==, and equals()]

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read K&B. Page 235 and 236, but it's not essential info.

I wrote something similar like their code. I'm confused.

Why:
produces:
== : true
!= : false
i1.equals(i2) : true

andproduces:
== : false
!= : true
i1.equals(i2) : true


[ September 21, 2007: Message edited by: Piotr Milewski ]
[ September 21, 2007: Message edited by: Piotr Milewski ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The integer values from -128 to 127 are stored into a pool just like String pool. So, for those values, when you do Integer i1 = 100; it is actually Integer.valueOf(100); which creates an Integer object with value as 100 and adds it to the pool. When Integer i2 = 100; is executed, an Integer object with value as 100 will be available in the pool and that will be used.

Hope this helps.
[ September 21, 2007: Message edited by: Vinayagar Karpagam ]
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer literals between -128 and 127 will result true comparing with ==.
 
Piotr Milewski
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.

So this "integer pool" contains only values between -128 and 127, correct? (not between 0 and 127) Am I correct?
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Per Java Language Specification

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.

 
Piotr Milewski
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that's enough to finish this case successfully

Thanks a lot!
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic