EasyConcept

What is the difference between == and equals() in Java?

Technology:
Java
Experience:
Intern,
Junior

Quick answer

== compares primitives by value and objects by reference (whether they are the exact same object), while equals() compares the logical content of two objects when the class overrides it.

Detailed explanation

For String this surprises many people: two strings with the same text created with new are different objects, so == is false even though equals() is true. String literals may be equal under == only because of the string pool, which you should never rely on.

The default equals() inherited from Object behaves like ==. If you override equals() in your own class, you must also override hashCode() so that equal objects have equal hash codes, otherwise they will be lost in a HashMap or HashSet.

Example

java
String a = new String("java");
String b = new String("java");
System.out.println(a == b);        // false - different objects
System.out.println(a.equals(b));   // true  - same content

Integer x = 127, y = 127;
System.out.println(x == y);        // true  (cached small values) - do not rely on it

Key points

  • == compares references for objects
  • equals() compares content if overridden
  • Override hashCode() whenever you override equals()

Follow-up questions

  • What is the equals and hashCode contract?
  • What is the String pool?
  • JavaEasy

    What is the difference between JDK, JRE and JVM in Java?

    The JVM runs Java bytecode, the JRE is the JVM plus the standard libraries needed to run Java programs, and the JDK is the JRE plus the development tools such as the compiler (javac).

    Intern 路 Junior 路 Programming Fundamentals
  • JavaEasy

    What is the difference between final, finally and finalize in Java?

    final is a keyword that prevents reassigning a variable, overriding a method or extending a class; finally is a block that always runs after try/catch for cleanup; finalize() was a method called by the garbage collector and is now deprecated.

    Intern 路 Junior 路 Programming Fundamentals
  • JavaMedium

    What is the difference between an abstract class and an interface in Java?

    An abstract class can have instance fields, constructors and a mix of implemented and abstract methods but supports only single inheritance, while an interface defines a contract that a class can implement multiple times and, since Java 8, can include default methods.

    Junior 路 Mid-level 路 Object-Oriented Programming
  • JavaMedium

    What is the difference between checked and unchecked exceptions in Java?

    Checked exceptions (subclasses of Exception other than RuntimeException) must be caught or declared with throws, while unchecked exceptions (RuntimeException and Error) do not have to be handled and usually signal programming bugs.

    Junior 路 Mid-level 路 Programming Fundamentals
  • JavaMedium

    What are the main features of Java 8: lambdas, streams and Optional?

    Java 8 introduced lambda expressions, functional interfaces, the Stream API for declarative collection processing, Optional to represent missing values, default methods in interfaces, method references and the java.time date and time API.

    Junior 路 Mid-level 路 Functions & Scope