PRO
Miki
asked

Expert
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.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
