---
title: "Explain the difference between == and equals() in Java."  
description: "Explain the difference between == and equals() in Java."  
author: "Ashutosh Patel"  
published: 2024-07-22  
updated: 2024-07-22  
canonical: https://www.mindstick.com/interview/33964/explain-the-difference-between-and-equals-in-java  
category: "java"  
tags: ["java", "java method"]  
reading_time: 4 minutes  

---

# Explain the difference between == and equals() in Java.

#### Difference Between ‘==’ and equal()

“**==**” and “**equals()**” are used to compare objects, but they serve different purposes,

## “==” (equality operator)

The `==` operator checks if the reference is equal. In other words, it checks whether two references refer to the same memory location, indicating that they do the same thing.

For primitive data types (such as `int`, `double`, etc.), `==` ensures that values ​​are equal.

## Example-

```java
String str1 = new String("hello");
String str2 = new String("hello");
String str3 = str1;
System.out.println(str1 == str2); // false, different objects
System.out.println(str1 == str3); // true, same object reference
```

\
**equals() method**

The `equals()` method is a method defined in the `Object` class to ensure value equality (and overridden in many classes such as `String` and `Integer`).\
By default, `equals()` of the `Object` class behaves like `==` (i.e., checks for reference equality). However, many classes override `equals()` to compare contents or values ​​of objects.

## Example-

```java
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1.equals(str2)); // true, contents are equal
```

## In example above

`equals()` compares the contents of `str1` and `str2` (i.e. their character sequences), and since they are both "**hello**", it returns true.

#### Key Differences

**Purpose-** `==` checks for reference equality or value equality for a primitive, while `equals()` is used to check content or value equality.\
**Usage-** Use `==` to check if two references point to the same object instance. Use `equals()` to check if two objects are logically equivalent based on their internal state.\
**Overriding-** While `==` cannot be overridden (because it is an operator), `equals()` can be overridden by classes to provide custom equality checks.

\
`==` Test whether two references point to the same thing.\
`equals()` checks that two objects are logically equivalent based on their context or state, as defined by the class of the `equals()` function.

**Also, Read:** [What is the significance of the 'final' keyword in Java?](https://www.mindstick.com/interview/33963/what-is-the-significance-of-the-final-keyword-in-java)

## Answers

### Answer by Ashutosh Patel

#### Difference Between ‘==’ and equal()

“**==**” and “**equals()**” are used to compare objects, but they serve different purposes,

## “==” (equality operator)

The `==` operator checks if the reference is equal. In other words, it checks whether two references refer to the same memory location, indicating that they do the same thing.

For primitive data types (such as `int`, `double`, etc.), `==` ensures that values ​​are equal.

## Example-

```java
String str1 = new String("hello");
String str2 = new String("hello");
String str3 = str1;
System.out.println(str1 == str2); // false, different objects
System.out.println(str1 == str3); // true, same object reference
```

\
**equals() method**

The `equals()` method is a method defined in the `Object` class to ensure value equality (and overridden in many classes such as `String` and `Integer`).\
By default, `equals()` of the `Object` class behaves like `==` (i.e., checks for reference equality). However, many classes override `equals()` to compare contents or values ​​of objects.

## Example-

```java
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1.equals(str2)); // true, contents are equal
```

## In example above

`equals()` compares the contents of `str1` and `str2` (i.e. their character sequences), and since they are both "**hello**", it returns true.

#### Key Differences

**Purpose-** `==` checks for reference equality or value equality for a primitive, while `equals()` is used to check content or value equality.\
**Usage-** Use `==` to check if two references point to the same object instance. Use `equals()` to check if two objects are logically equivalent based on their internal state.\
**Overriding-** While `==` cannot be overridden (because it is an operator), `equals()` can be overridden by classes to provide custom equality checks.

\
`==` Test whether two references point to the same thing.\
`equals()` checks that two objects are logically equivalent based on their context or state, as defined by the class of the `equals()` function.

**Also, Read:** [What is the significance of the 'final' keyword in Java?](https://www.mindstick.com/interview/33963/what-is-the-significance-of-the-final-keyword-in-java)


---

Original Source: https://www.mindstick.com/interview/33964/explain-the-difference-between-and-equals-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
