difference betweeen == and .equals() in java code example
Example: difference betweeen == and .equals() in java
public class Test {
public static void main(String[] args)
{
String s1 = new String("HELLO");
String s2 = new String("HELLO");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
Output:
false
true
Explanation:
We can use == operators for reference comparison (address comparison) and
.equals() method for content comparison. In simple words, == checks if both
objects point to the same memory location whereas .equals() evaluates to the
comparison of values in the objects.