== is a relational operator that returns true when both the string reference variables under comparison are referring to the same object. == simply looks at the bits in the variable, and they're either identical or they're not.
equals is a method defined in the Object class. The default implementation of the method provided in this class is similar to "== " equality operator and returns true if we are comparing two references of the same object. But this method is overridden by String class in such a way that when equals () of the String class is invoked, the contents of the String objects rather than the references are compared and a boolean value is returned accordingly i.e. if both the String objects contain the same sequence of characters, true is returned else false .
equals vs String Comparison CODE import java.lang.*; OUTPUT Not Equal DESCRIPTION In THINGS TO TRY
When you really need to know if two references are same and point to the same object, use ==. But when
you need to know if the objects themselves (not the references) are equal, use the
equals ()
|