• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer class  RSS feed

 
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the response to this program:



Why the response is different?

Thank you for your time.


 
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoting from JLS
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.


Having said that, you shouldn't use "==" with objects. Two objects should be checked for equality as i1.equals(i2)
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't rely on that behaviour. It is possible to set options which will cause 128 to be cached similarly. Have a look at Integer#valueOf(int) as well as the JLS link which SF gave you. Beware: the JLS can be difficult to read.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!