Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Difference between == and .equals() Method in Java

Last Updated on October 16, 2023 by Ankit Kochar

In the realm of programming, it is quite common to find the need to evaluate two values or contrast two sets of data. Java offers two distinct methods to achieve this functionality, namely, the "==" operator and the "equals" method, both of which facilitate the comparison of either values or objects.

Let us first learn about == and equals() method in Java before discussing the differences between the two.

== Operator in Java

Within the Java programming language, the "==" operator serves the purpose of comparing variables belonging to primitive data types as well as comparing objects within the realm of non-primitive data types. It’s important to note that, since it’s an operator, it cannot be overridden in Java. This operator yields a boolean result, signifying that the outcome can only be either true or false.

Generally, Primitive Datatypes such as int, long, short, byte, float, double, etc, are compared using the == operator.

Let us see the working of this operator using an example.

class Main
    public static void main(String[] args)
        int n1 = 10;
        int n2 = 20;
        // comparing n1 with n2
        System.out.println("Comparing n1 and n2: " + (n1 == n2));

        int n3 = 10;
        int n4 = 20;
        int n5 = 2 * n3; // n5 = 20

        // comparing n4 with n5
        System.out.println("Comparing n4 and n5: " + (n4 == n5));


Comparing n1 and n2: false
Comparing n4 and n5: true

We are defining five integers here. The first comparison shows that n1 is 10 and n2 is 20, indicating that the result is erroneous.
In the second comparison, we can see that because n4 and n5 are both 20, the result is true. Moreover, we have used the additional variables to demonstrate that the == operator does not compare memory locations.

.equals() Method in Java

In Java, the .equals() method serves the purpose of comparing the values of two objects to determine their equality. This method is originally defined in the Object class and can be customized or overridden by subclasses to offer a more specific comparison tailored to their needs. The outcome of this method is a boolean value, which indicates whether the two objects are considered equal or not.

For Example

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1.equals(s2));  // Outputs: true

It’s important to note that the == operator in Java checks for reference equality, meaning it checks if two variables refer to the same object in memory. In contrast, the .equals() method checks for value equality, which means it checks if the values stored in two objects are the same.

Examples of Usage of .equals() Method

These examples show how to use the .equals() method in java in various situations:

Example 1: Comparing two objects where the second object is assigned to the same memory location as the first one

class DemoObject {

  int x;

  DemoObject(int x) {
    this.x = x;

  public static void main(String[] args) {
    // creating two objects
    DemoObject obj1 = new DemoObject(5);
    DemoObject obj2 = obj1;
    // hashcode of obj1
    System.out.println("obj1 = " + obj1);
    // hashcode of obj2
    System.out.println("obj2 = " + obj2);
    // comparing the two objects
    System.out.println("Comparing both the objects = " + (obj1.equals(obj2)));


obj1 = DemoObject@548c4f57
obj2 = DemoObject@548c4f57
Comparing both the objects = true

Here, we define obj1 with the value x=5 and then obj2 with obj1’s location. Then we output the objects that include the hashcodes of obj1 and obj2. Because the positions of both items are the same, the comparison yields true.

Example 2: Comparing two Java objects with same values, but pointing to different memory locations.

class Main {

  int x;

  Main(int x) {
    this.x = x;

  public static void main(String[] args) {
    // creating two objects
    Main obj1 = new Main(5);
    Main obj2 = new Main(5);

    // hashcode of obj1
    System.out.println("obj1 = " + obj1);

    // hashcode of obj2
    System.out.println("obj2 = " + obj2);

    // comparing the two objects
    System.out.println("Comparing both the objects = " + (obj1.equals(obj2)));


obj1 = Main@548c4f57
obj2 = Main@1218025c
Comparing both the objects = false

Here in this example, we’re making two objects with the identical x=5 value. However, because the locations of the two items differ, as seen by the output, we receive false as the result of the comparison.

Overriding .equals() Method in Java

In Java, the .equals() method is defined in the Object class and is used to compare two objects for equality. By default, the .equals() method compares object references to determine if two objects are equal.

However, in some cases, it may be necessary to override the .equals() method to provide a custom implementation that better suits the needs of a particular class. For example, you may want to override the .equals() method to compare two instances of a custom class based on their properties instead of their references.

Here is an example of how to override the .equals() method in Java:

class Person {
  private String name;
  private int age;
  public Person(String name, int age) { = name;
    this.age = age;

  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Person person = (Person) o;
    return age == person.age && name.equals(;
  public static void main(String args[]){
  	Person p1 = new Person("Prep", 26);
  	Person p2 = new Person("Prep", 26);
  	Person p3 = new Person("Buddy", 23);
  	System.out.println("Comparing p1 and p2 : " + p1.equals(p2));
  	System.out.println("Comparing p1 and p3 : " + p1.equals(p3));


Comparing p1 and p2 : true
Comparing p1 and p3 : false

In this example, the .equals() method compares two instances of the Person class based on their name and age properties. The method first checks if the object references are equal, and if so, returns true. Next, it checks if the passed object is null or if its class is not the same as the current object’s class. If either of these conditions is met, the method returns false. Finally, the method casts the passed object to a Person object and compares the name and age properties for equality. Then, we declare three objects of class Person and used the overridden method to show output on the console screen.

.equals() Method with String in Java

Within Java, the .equals() method finds utility in comparing two strings to ascertain their equality. It produces a boolean result, returning "true" if the strings are identical and "false" if they differ. This method conducts a character-by-character comparison in the correct order. Consequently, strings like "hello" and "hello" would yield "true" as a result, while "hello" and "heLlo" would lead to "false" due to the differing character casing.

public class Main{

  public static void main(String[] args) {
    String st1 = "Prep";
    String st2 = "pre";
    // comparing st1 with st2
    System.out.println("Comparing st1 and st2: " + (st1.equals(st2)));

    String st3 = "Bytes";
    String st4 = "PrepBytes";
    String st5 = st1 + st3; // st5 = "PrepBytes"

    // comparing st4 with st5
    System.out.println("Comparing st4 and st5: " + (st4.equals(st5)));


Comparing st1 and st2: false
Comparing st4 and st5: true

We’ve specified five strings here. Because st1 and st2 do not match while maintaining case sensitivity, the result is false.Now we will compare st4 with st5. Because st4 and st5 match char for char, the result is a true value.
It demonstrates that equals() compares the contents of the string objects as well as their location.

Difference between == and equals in Java

Although both == and .equals() are used for comparing two primitive and non-primitive datatypes, they differ a lot. The table given below enlists the key differences between == and equals in java:

Basis \== Operator .equals() Method
Purpose Check for reference equality Check for value equality
Usage obj1 == obj2 obj1.equals(obj2)
Primitive Types Compares values N/A
Reference Types Compares references Compares values if overridden, otherwise compares references
Null Reference Throws NullPointerException Returns false
Overloading Cannot be overloaded Can be overloaded
Performance Faster than .equals() Slower than ==
Inheritance Cannot be overridden Can be overridden in subclasses to change behavior
Best Use Case Checking for same instance Checking for equal values
Recommended for Primitive types, performance-critical code Reference types, meaningful equality comparison

So, in this article, we have learned about == and equals in Java. We see the working and behavior of both in various different situations. We have discussed how we can override the .equals() method in java. After that, the differences between == and equals in java have been enlisted. Hope you have enjoyed and learned the topic.

Frequently Asked Questions (FAQs) related to the Difference between == and .equals() Method in Java

Below are some of the FAQs related to the Difference between == and .equals() Method in Java.

1. What is the primary difference between "==" and ".equals()" in Java for comparing objects?

Answer: The "==" operator compares object references, checking whether two references point to the same memory location. ".equals()" method, on the other hand, compares the actual contents or values of objects.

2. When should I use "==" in Java?

Answer: Use "==" when you want to check if two object references point to the same memory location. It’s typically used for comparing primitive data types and checking if two references are identical.

3. When should I use ".equals()" in Java?

Answer: Use ".equals()" when you want to compare the content or values of objects, especially when dealing with non-primitive data types like strings or custom objects. It allows for a more meaningful comparison.

4. Can I use "==" to compare strings in Java?

Answer: Yes, you can use "==" to compare strings, but it will check if the references to the string objects are the same, not whether the strings have the same content. For content comparison, you should use ".equals()".

5. Why is it important to override ".equals()" method for custom classes?

Answer: When you create custom classes, you often want to compare instances based on their content, not just their memory locations. Overriding ".equals()" allows you to define how instances of your class should be compared.

Leave a Reply

Your email address will not be published. Required fields are marked *