Difference Between == and equals() in Java

Difference Between == and equals() in Java

In this article, I shall describe two basic equality checks in Java – reference equality and value equality. We'll compare them, show examples, and highlight the key differences between them.



Reference Equality

In Java, "==" is used for reference equality, which means that it checks whether two objects refer to the same memory location.

Here's an example:

String s1 = "hello";
String s2 = new String("hello");

if (s1 == s2) {
  System.out.println("s1 and s2 are the same object");
} else {
  System.out.println("s1 and s2 are different objects");
}

Output:

s1 and s2 are different objects

Description:

In this example, we create two String objects: s1 is created using a string literal, while s2 is created using the new keyword.

We use == to compare s1 and s2, we get the output "s1 and s2 are different objects" because s1 and s2 are stored in different memory locations, even though they have the same value.



Value Equality

Value equality takes place when two separate objects happen to have the same values or state.This compares values and is closely related to the Object's equals() method.

Here's an example:

String s1 = "hello";
String s2 = new String("hello");

if (s1.equals(s2)){ 
  System.out.println("s1 and s2 have the same value");
} else {
  System.out.println("s1 and s2 have different values");
}

Output:

s1 and s2 have the same value

Description:

In this example, we create two String objects: s1 is created using a string literal, while s2 is created using the new keyword.

We use equals to compare s1 and s2, we get the output "s1 and s2 have the same value" because the contents of s1 and s2 are the same.


Conclusion:

So, the main difference between "==" and "equals" in Java is that "==" compares the memory location of two objects, while "equals" compares the contents of two objects.


Bhavya Jha

__Aspiring Computer Programmer | first year computer science student at SRIT

2mo

Very useful...i just solved a question on codeforces about this topic...single element in sorted array.. while doing so i initially used equals to(==) operator and found myself stuck with only half of the test cases passed..but when i tried using value equality (.equals), all the test cases passed.. So now i understand the concept behind the problem with my initial code.. Thankyou Sir for the clarity in concept..

Like
Reply
Rafael Miguel

QA | Automação | Java | Selenium | Cucumber

2mo

Thank you so much. I've always used == instead equals in a if else, and now i know the difference

Like
Reply
Farid Ahmad Haidary

Attended Kabul University

3mo

Thank you very much it was very helpful

Like
Reply
Muhammad Omar Aslam

Freelance Software Developer

1y

I was encountering this problem today 😂and fixed it using equals thingy

Babar Shahzad Thank you for sharing! its very informative.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics