Miki Poveda
PRO
2 months ago
Mikicountry asked

What’s the difference between == and .equals() in Java?

Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Miki,

Great question — this is a common source of confusion in Java.

  • == checks if two variables point to the exact same object in memory. It doesn’t care about the content — just the reference.

  • .equals() checks if the content of two strings is the same, even if they’re different objects.

Example:

String a = new String("hello");
String b = new String("hello");

System.out.println(a == b);       // false — different objects
System.out.println(a.equals(b));  // true — same content

So whenever you want to compare the actual text in two strings, always use .equals().

If you have more questions, I am here to help.

Java
This question was asked as part of the Practice: Java Basics course.