• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

== and equals  RSS feed

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code snippet :

Integer i1 = 1000;
Integer i2 = 1000;

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

Produces output :

different objects
meaningfully equal

However,

Integer i1 = 10;
Integer i2 = 10;

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

Produces output :

same objects
meaningfully equal

Could someone explain the output in both scenarios ?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has been asked many times so a quick search will definitely help.
 
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. This is one of the things which comes up on such a search.
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!