1// Java program to understand
2// the concept of == operator and .equals() method
3public class Test {
4 public static void main(String[] args)
5 {
6 String s1 = new String("HELLO");
7 String s2 = new String("HELLO");
8 System.out.println(s1 == s2);
9 System.out.println(s1.equals(s2));
10 }
11}
12
13Output:
14false
15true
16
17Explanation:
18We can use == operators for reference comparison (address comparison) and
19.equals() method for content comparison. In simple words, == checks if both
20objects point to the same memory location whereas .equals() evaluates to the
21comparison of values in the objects.