This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wrappers

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Check the following program:

public class Temp
{

public static void main(String[] args)
{
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects i1 i2");
if(i1.equals(i2)) System.out.println("meaningfully equal i1 i2");

Integer i3 = 10;
Integer i4 = 10;
if(i3 != i4) System.out.println("different objects i3 i4");
if(i3.equals(i4)) System.out.println("meaningfully equal i3 i4");

}
}

The output is:

different objects i1 i2
meaningfully equal i1 i2
meaningfully equal i3 i4

Please clarify.Why i1 and i2 are different objects when both are Integer and have value 1000 which is in the range of int.

Thanks
 
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 rakhee gupta:
...Why i1 and i2 are different objects when both are Integer and have value 1000 which is in the range of int...


To get the same reference when autoboxing, the values need to be within the range of a byte.

JLS 5.1.7 - Boxing Conversion...

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.


My bold.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic