Answers for "difference between compare to and equals in java"

1

difference betweeen == and .equals() in java

// Java program to understand 
// the concept of == operator and .equals() method
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.
Posted by: Guest on March-31-2021

Code answers related to "difference between compare to and equals in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language