• 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

Topic related to == and equals

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers,
I am not able to understand the following example:

Integer i1 = 1000;
Integer i2 = 1000;

if (i1 != i2)
System.out.println("different objects");

if (i1.equals(i2))
System.out.println("meaningfully equal");

Integer i3 = 10;
Integer i4 = 10;
if (i3 == i4)
System.out.println("same object");
if (i3.equals(i4))
System.out.println("meaningfully equal");

if it comparing based on the value then why in 1 case it says equal and in other not equal.
Please help
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out our FAQ, it covers precisely this question: https://coderanch.com/how-to/java/Java-FAQ#integerAutoBoxing
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic