Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Apr 12, 2002

The Difference between "==" and "equals"


The basic difference between the "==" and the "equals" method is to compare the two entities on either side of the operator or the method. But when objects are compared, the "==" checks for the memory address equality while the "equals" method checks the content equality. If the equals method is used as such, it will just compare the address equality (which is the function of the "==" operator). Hence to make the equals method compare the content it has to be overridden (either at API level or at the user code level). Basically, if class A is not overriding the equals method then:
 	
A a = new A();
A b = new A();

a.equals(b) and a==b will return false as their memory addresses are different. Whereas if the equals method is overridden in class A to implement the comparing algorithm the result is:
 
a.equals(b)       will return true
a==b              will return false
Karthikeyan Palani
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date