Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

== and equals()

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer x = new Integer (22);
Integer y = new Integer (22);
if(x == y) System.out.println ("x == y is true"); else System.out.println ("x==y is false");
if(x.equals(y)){System.out.println ("x.equals(y) is true");} else System.out.println ("x.equals(y) is false");

the output is
---------- Capture Output ----------
x==y is false
x.equals(y) is true
> Terminated with exit code 0.

I doubt about x and y have the same value, why it turns out false?
x and y is true, because they share the same data type, or based on same value 22?
 
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
== is a simple comparison of values. For primitives, this is straight forward. For references, it compares whether the same object is referenced.

When you create instances using "new," you are creating distinct objects. Even if two instances might be considered "equal" according to how their equals method is implemented, they are separate objects and == will indicate this.
[ September 18, 2007: Message edited by: marc weber ]
 
It's exactly the same and completely different as this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic