Difference Between .equals() and == in Java

Difference Between .equals() and == in Java

5 mins readComment
clickHere
Esha
Esha Gupta
Associate Senior Executive
Updated on Mar 13, 2024 15:31 IST

Have you ever wondered about the difference between .equals() and == in Java? While == is used for comparing references or primitive values to see if they point to the same object or have the same value, .equals() is designed to compare the contents or state of two objects, offering a deeper level of equality checking that can be customized in user-defined classes. Let's understand more!

In Java .equals() and == are both used for comparison, but they serve different purposes and work in distinct ways. In this blog, we will understand the major differences between them in detail!

Java String Compare: A Guide to Effective String Comparison in Java
Java String Compare: A Guide to Effective String Comparison in Java
Have you ever wondered how to compare strings in Java? You can use various methods for that, like using equals(), using == Operator, using equalsIgnoreCase(), using compareTo() and using compareToIgnoreCase()....read more

Table of Content

 

Difference Between .equals() and == in Java

Below is a table differentiating between .equals() and == in Java.

Aspect

==

.equals()

Purpose

Used to compare primitives or check if two references point to the same object.

Used to compare the content or state of two objects.

Usage

For primitive data types, it compares the values. For object references, it compares the reference locations (memory addresses).

Compares the values stored in two objects, based on the implementation of the .equals() method in the object's class.

Default Behavior

For objects, checks reference equality. For primitives, checks value equality.

In the Object class, it behaves the same as ==, checking reference equality. However, it is often overridden in child classes to check content equality.

Overridable

No, == is an operator and its behaviour cannot be changed.

Yes, Classes can override the .equals() method to define their own criteria for equality.

Primitive Comparison

Applicable and compares values directly.

Not applicable directly to primitives. Primitives must be wrapped as objects (e.g., Integer, Character) to use .equals().

Object Comparison

Checks if two reference variables point to the same object instance.

Checks if the contents of two objects are logically equivalent, depending on the implementation of .equals().

What is .equals() in Java?

The .equals() method in Java is used to compare two objects for equality based on the data or content of the objects rather than their reference identities. By default, the .equals() method in the Object class compares the memory addresses or references of the objects, essentially checking if the two references point to the same object in memory. However, the .equals() method is often overridden in user-defined classes to perform a content-based comparison, checking if the data within two objects is the same.

For example,

In the String class .equals() is overridden to compare the sequence of characters in two strings. If the sequences of characters are the same .equals() returns true; otherwise, it returns false.

Example demonstrating its use:


 
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
// Using .equals() to compare content of strings
boolean result1 = str1.equals(str2); // true, because their contents are the same
boolean result2 = str1.equals(str3); // true, even though str3 is a different object
System.out.println(result1); // Outputs true
System.out.println(result2); // Outputs true
Copy code

This behaviour is particularly useful when you need to compare the values of objects for equality rather than their identities, which is what the == operator does by default.

What is == in Java?

The == operator in Java is used to compare the references of two objects, not their contents. It checks whether two references point to the same object in memory, meaning it's used to determine if two variables refer to the same object instance.

For primitive data types (like int, char, float, etc.), the == operator compares the values stored in the variables. However, for objects, it compares the memory addresses or references of the objects, not the actual data or contents of the objects.

Example to illustrate its use with objects:


 
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
// Using == to compare references
boolean result1 = str1 == str2; // true, because they refer to the same instance in the string pool
boolean result2 = str1 == str3; // false, because str3 is a new object with a different memory address
System.out.println(result1); // Outputs true
System.out.println(result2); // Outputs false
Copy code

An example with primitive data types:


 
int a = 5;
int b = 5;
// Using == to compare primitive values
boolean result = a == b; // true, because the values of a and b are the same
System.out.println(result); // Outputs true
Copy code

Array Programs in Java | Beginner to Expert Level
Array Programs in Java | Beginner to Expert Level
Array programs in Java traverse from basic single-dimensional arrays to complex multi-dimensional arrays and dynamic arrays using ArrayList. From initializing and accessing array elements, to advanced operations like sorting and...read more

Difference Between Array and Vector in Java
Difference Between Array and Vector in Java
Have you ever wondered about the fundamental differences between an array and a vector in Java? An array is a fixed-size collection of elements of the same type, offering fast...read more

Relational Operators in Java | About, Types and Examples
Relational Operators in Java | About, Types and Examples
Have you ever wondered how Java programs make decisions? Relational operators like ==, !=, >, =, and ...read more

Understanding User Defined Exception in Java
Understanding User Defined Exception in Java
Have you ever wondered how to handle unique error scenarios in Java that standard exceptions don't cover? User-defined exceptions allow you to create specific error types for these situations, providing...read more

Difference Between String and StringBuffer in Java
Difference Between String and StringBuffer in Java
In Java, String is immutable, meaning it cannot change once created, making it less efficient for frequently modified text. Conversely, StringBuffer is mutable and thread-safe, ideal for strings undergoing frequent...read more

Thus, the == operator is straightforward and efficient for comparing primitive values or checking if two variables refer to the exact same object instance. However, for object content comparison, it's recommended to use the .equals() method, as it can be overridden to compare the data within the objects.

Check out Java Courses here to learn more!

FAQs

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

In Java, == is an operator that compares the references or memory addresses of objects to determine if they are the same, whereas .equals() is a method that compares the contents of the objects to check for value equality.

How does == behave with primitive types compared to .equals()?

For primitive types like int, char, float, etc., == compares the actual values, since primitives do not have methods. The .equals() method is not applicable to primitives directly; it is used with objects.

Can .equals() be overridden, and how does it affect comparison?

Yes, .equals() can be overridden. By default, the .equals() method in the Object class compares the references, just like ==. However, it can be overridden to compare the actual content or state of an object, as done in classes like String, Integer, etc.

Why does == return false when comparing two distinct String objects with the same content?

The == operator compares the memory addresses of the two String objects. If they are two distinct objects (i.e., not interned strings), they will have different addresses in memory, hence == will return false, despite their content being the same.

Is there a scenario where .equals() and == will behave the same for object comparison?

If two reference variables point to the exact same object in memory, both .equals() and == will return true. This is because the default implementation of .equals() behaves the same as ==, and if an object's .equals() method is not overridden, it will compare references.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio