This code snippet
describes the difference between equals() and equal(==) operator in
java.
String str1="Hello";
String str2="Hello";
String str3=new String("Hello") //Using constructor.
If(str1 == str2)
System.out.println("Equal 1");
else
System.out.println("Not Equal 1");
If(str1 == str3)
System.out.println("Equal 2");
else
System.out.println("I am constructed using constructor, hence not interned");
If( str1.equals(str3) )
System.out.println("Equal 3");
else
System.out.println("Not Equal 3");
Output of the Above Programs
Equal 1
Not Equal 2
Equal 3