• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

What is the difference between operator == and method equals()?

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

could anyone tell me the difference between operator == and method equals()?

Thanks

Sura
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The == operators checks if both object references "refer" to the same object.
the equals method checks if two objects have the same values and are at the same inheritance tree.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The == operator is used to check whether reference variables refer to the same object.

the equals() method checks the equality of the object, it checks whether both the instance have same characters in the same sequence.


class A {

public static void main(String args[]) {


A a="Have a good day";
A b="Have a good day";

A a1=new A("Have a good day");


System.out.println((a==b)); // True
System.out.println(a.equals(b)); // True
System.out.println((a1==b)); // False
System.out.println(a1.equals(b)); // True

}
}

Regards
Vishnu
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies ...
The example provided does not provide the complete solution. Actually equals() method a method which would check the hashcode of the object and since all the class inherit either directly or indirectly so they do inherit this method.

Since String class has overridden this method such that equals() method checks for the content and hence it would work but if u have any other class we need to override this method according to our need : I am quoting the example provided in the Thinking in Java : ( An excellent book for concepts )

class Value {
int i;
}
public class EqualsMethod2 {
static Test monitor = new Test();
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
});
}
}

See the difference in results when we have String and any other class.

Cheers,
Murali
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It�s important to note that the equals method in the Object class does the same that the == operator do. It checks if the variables reference the same object in the heap. So unless you override the equals methods in your class to do something smarter, for example comparing the instance variables of the objects, a call to equals() is identical to using ==.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic