Java Interview Questions

30 Questions
Java

Java

Web DevelopmentBackend

Question 8

What is the difference between == and equals() method?

Answer:

In Java, == and equals() are used to compare objects, but they serve different purposes and operate differently.

== Operator

Definition: The == operator compares the reference or memory location of two objects to determine if they refer to the same object.

Usage:

  • Used for comparing primitive data types.
  • Used to check if two reference variables point to the same object in memory.

Example with Primitive Types:

int a = 5;
int b = 5;
System.out.println(a == b);  // Output: true

Example with Objects:

String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);  // Output: false (different memory locations)

String s3 = s1;
System.out.println(s1 == s3);  // Output: true (same memory location)

equals() Method

Definition: The equals() method is used to compare the contents or values of two objects. It is a method defined in the Object class and can be overridden by any class to provide custom comparison logic.

Usage:

  • Used to compare the actual content of objects for logical equality.
  • The default implementation in the Object class compares references (similar to ==), but many classes (like String, Integer, etc.) override it to compare values.

Example with Strings:

String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1.equals(s2));  // Output: true (content is the same)

String s3 = "hello";
String s4 = "hello";
System.out.println(s3.equals(s4));  // Output: true (content is the same)

Key Differences

  1. Comparison Type:

    • == compares memory addresses (references) for objects.
    • equals() compares the actual content or value of the objects.
  2. Primitive Types:

    • == can be used to compare primitive types directly.
    • equals() is not used for primitive types; it is only for objects.
  3. Overriding:

    • == cannot be overridden.
    • equals() can be overridden to provide custom comparison logic.
  4. Default Behavior:

    • For objects, == checks if both references point to the same object.
    • The default equals() method in the Object class behaves similarly to ==, but it is commonly overridden to check for value equality.

Custom Class Example

Default Behavior:

class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class Main {
    public static void main(String[] args) {
        Person p1 = new Person("John", 25);
        Person p2 = new Person("John", 25);
        System.out.println(p1 == p2);          // Output: false (different memory locations)
        System.out.println(p1.equals(p2));     // Output: false (default implementation, compares references)
    }
}

Overriding equals():

class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Person person = (Person) obj;
        return age == person.age && name.equals(person.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Person p1 = new Person("John", 25);
        Person p2 = new Person("John", 25);
        System.out.println(p1 == p2);          // Output: false (different memory locations)
        System.out.println(p1.equals(p2));     // Output: true (content is the same)
    }
}

Summary

  • Use == to check if two reference variables refer to the same object.
  • Use equals() to check if two objects have the same content.
  • Override equals() in custom classes to define what it means for instances to be "equal" based on their content.

Recent job openings