Java Program to Differentiate String ‘==’ operator and equals() method

Program to Differentiate String == operator and equals() method

What are Strings?

A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;

In this Article, we will write a program to Differentiate String ‘==’ operator and equals() method.

Strings:

We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.

What is ‘==’ Operator?

In Java, the “==” operator is used to compare the equality of two values. It compares the memory addresses of the objects to determine if they are the same object. For example, if two variables point to the same object in memory, the == operator will return true when used to compare the two variables. However, if two variables point to different objects with the same values, the == operator will return false.

Java equals() method :

In Java, the equals() method is a method of the Object class, which is inherited by all classes in Java. It is used to determine if two objects are equal based on their contents, rather than their memory addresses. The default implementation of the equals() method compares the memory addresses of two objects using the == operator, but it can be overridden by subclasses to implement their own logic for determining equality.
It is generally recommended to override the equals method in custom classes, to define the equality based on the business logic.

For example, if you have a class called Person that has a name and age field, you might override the equals() method to compare the contents of the name and age fields of two Person objects to determine if they are equal.

Program to Differentiate String ‘==’ operator and equals() method :

Run
import java.util.*;

public class Main {
  public static void main(String[] args) {
      
    // Initializing two Strings
    String string1 = new String("PrepInsta");
    String string2 = new String("PrepInsta");

    // using == operator
    boolean ans1 = (string1 == string2);
    System.out.println("Answer using '==' Operator : " + ans1);

    // using equals() method
    boolean ans2 = string1.equals(string2);
    System.out.println("Answer using equals() method : " + ans2);
  }
}
Output:

Answer using '==' Operator : false
Answer using equals() method : true

Explanation :

In above example, we have used the == operator and equals() method to check if two strings are equal.
“==” operator is used to compare the equality of two values. It compares the memory addresses of the objects to determine if they are the same.
the equals() method is a method of the Object class, which is inherited by all classes in Java. It is used to determine if two objects are equal based on their contents, rather than their memory addresses.

Differentiate String ‘==’ operator and equals() method using class :

Run
class Person {
    private String name;
    private int age;
 
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
 
        if (!(obj instanceof Person)) {
            return false;
        }
 
        Person person = (Person) obj;
        return person.name.equals(this.name) && person.age == this.age;
    }
}

class Main {
    public static void main(String[] args) {
        Person person1 = new Person("John", 25);
        Person person2 = new Person("John", 25);
        Person person3 = new Person("Jane", 30);
        
        System.out.println("person1 == person2: " + (person1 == person2));
        System.out.println("person1.equals(person2): " + person1.equals(person2));
        System.out.println("person1.equals(person3): " + person1.equals(person3));
    }
}

Output:

person1 == person2: false
person1.equals(person2): true
person1.equals(person3): false

Explanation :

In this example, the program creates three Person objects with different name and age fields. When the program runs, it uses the == operator to compare person1 and person2, and it uses the equals() method to compare the contents of person1 and person2, and person1 and person3.

As you can see, the == operator returns false because person1 and person2 are different objects in memory, even though they have the same name and age fields. The equals() method, on the other hand, returns true because it compares the contents of the objects and finds that they are the same. It also shows that person1 and person3 are not equal.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription