Explain the equals() method of the Object, String and, StringBuffer classes.

Java 8Object Oriented ProgrammingProgramming

To compare two objects the object class provides a method with name equals(), this method accepts an object and compares it with the current object. If the references of these two objects are equal, then it returns true else this method returns false.

Example

In the following example we have a class Employee with two variables name, age and a parameterized constructor.

From the main method we are creating two objects by passing same values and, comparing both values using the equals() method.

Since the equals() method of the Object class returns true only if the references of the two objects are equal, this program returns false.

 Live Demo

import java.util.Scanner;
class Employee {
   private String name;
   private int age;
   Employee(String name, int age){
      this.name = name;
      this.age = age;
   }
}
public class EqualsExample {
   public static void main(String[] args) {
      Scanner sc =new Scanner(System.in);
      System.out.println("Enter your name ");
      String name = sc.next();
      System.out.println("Enter your age ");
      int age = sc.nextInt();
      Employee emp1 = new Employee(name, age);
      Employee emp2 = new Employee(name, age);
      //Comparing the two objects
      boolean bool = emp1.equals(emp2);
      System.out.println(bool);
   }
}

Output

Enter your name
Krishna
Enter your age
20
false

The equals() method of the String class

The equals() method of the String class is not same as the equals() method of the Object class. It is overridden, this method accepts a String value and compares it with the current object and returns true only if the character sequences in the both String objects are exactly same.

Example

In the following example we are accepting values from the user, creating two String objects and comparing them using the equals() method and displaying the results.

Since the equals method returns true only if you enter same character sequences. You need to enter same values to print true.

 Live Demo

import java.util.Scanner;
public class StringEquals {
   public static void main(String[] args) {
      Scanner sc =new Scanner(System.in);
      System.out.println("Enter a name ");
      String name1 = sc.next();
      System.out.println("Enter another name ");
      String name2 = sc.next();
      //Comparing the two objects
      boolean bool = name1.equals(name2);
      System.out.println(bool);
   }
}

Output

Enter a name
Krishna
Enter another name
Krishna
true

The equals() method of the StringBuffer class

But, unlike the Sting class the StringBuffer does not override the equals() method. Its functionality is same as in the Object class.

Therefore, to get true you need to compare references pointing to the same value using the equal method.

Example

 Live Demo

import java.util.Scanner;
public class StringEquals implements Cloneable{
   public static void main(String[] args) {
      Scanner sc =new Scanner(System.in);
      System.out.println("Enter a name ");
      StringBuffer name1 = new StringBuffer(sc.next());
      StringBuffer name2 = name1;
      //Comparing the two objects
      boolean bool = name1.equals(name2);
      System.out.println(bool);
   }
}

Output

Enter a name
krishna
true
raja
Published on 05-Jul-2019 11:49:22
Advertisements