Difference between == and equals() method for a String

Result of using == and equals() method between two String objects is different.
== checks whether both String objects are referring to same address location.
equals() method checks whether the contents of both String objects are same.

public class Stringequals{

public static void main(String args[])
{
	String s1 =  new String("Hello");
	String s2 = new String("Hello");

	String str="Hello";

	String str1="Hello";

	//s2 and s3 refer to same address location
	String s3 = s2;

	if(s1==s2)//== checks address location
	{
		System.out.println("if(s1==s2)");
	}
	else
	{
		System.out.println("else (s1==s2)");
	}

	if(s2==s3)//== checks address location
	{
		System.out.println("if(s2==s3)");
	}
	else
	{
		System.out.println("else (s2==s3)");
	}

	if(s1.equals(s2))//equals checks contents of s1, s2
	{
		System.out.println("if s1 equals s2");
	}
	else
	{
		System.out.println("else s1 equals s2");
	}

	if(str==str1){
		System.out.println("if(str==str1)");
	}
}
}


Output:
else (s1==s2)
if(s2==s3)
if s1 equals s2
if(str==str1)

You may also like to read:
Inherit From String class
How to override toString() method
Difference between String and StringBuilder
String object creation with and without new