The equals() method and == operator in Java perform two different operations. The equals() method is used to compare two string values. On the other hand == operator is used to compare two primitive datatype variables or to compare references of two objects. Below is a sample program that shows difference between equals() and == in Java.
public static void main(String...s)
String str1=new String("The Crazy Programmer");
String str2=new String("The Crazy Programmer");
System.out.println("str1 equals() str2 "+str1.equals(str2));
System.out.println("str1 == str2 "+(str1==str2));
In first case we are comparing the values or content of String objects str1 and str2 using equals() method. The result is true because there values are equal.
In the second case we are comparing the references of String objects str1 and str2 using == operator. The result is false because there references are not equal as both the objects are different.