Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Java General Java The equals() method Tutorial

We can use the == operator to compare the values of primitive variables and determine if they are equal. However, if object reference variables are compared using the == operator, it returns true only if the reference variables are referring to the same o

Tutorial Details:

The equals() method

We can use the == operator to compare the values of primitive variables and determine if they are equal. However, if object reference variables are compared using the == operator, it returns true only if the reference variables are referring to the same object.

To check for the equality of two objects, the Object class provides the equals(Object obj) method, which can be overridden to return true for logically equal objects. The default implementation of the equals() method in the Object class returns true only if an object is compared to itself -- that is, it behaves similarly to the == operator.

Classes like String and Boolean have overridden the equals() method, while StringBuffer has not. For instance:

String s1 = "abc";
String s2 = new String("abc");
if (s1 == s2) // returns false
System.out.println("Same Object Reference");
if (s1.equals(s2)) // returns true
System.out.println("Equal Objects"); // prints "Equal Objects"


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Java General Java The equals() method Tutorial

View Tutorial:
Java General Java The equals() method Tutorial

Related Tutorials:

Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.