Member-only story
Java equals() vs == – Understanding the Key Differences
“Programming isn’t about what you know; it’s about what you can figure out.” — Chris Pine
When you’re learning Java, one of the most common areas of confusion is the difference between the equals() method and the == operator. At first glance, both seem to compare two objects, but they function very differently under the hood. Understanding the distinction is crucial because using one in place of the other can lead to unexpected results in your code.
In this blog, we’ll break down the key differences between equals() and ==, explore when to use each, and provide practical examples to illustrate their behavior.
What is == in Java?
The == operator in Java is used to compare primitive types like int, char, or boolean. However, when applied to reference types (like objects), it checks whether two references point to the same memory location.
In other words, == determines if two object references are identical, not whether their contents or values are the same.
Example:
public class Main {
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1 == str2) {
System.out.println("Both…










