str2.equals(s1: String): boolean
class equals
{
public static void main(String[] arg)
{
String str1 = "Welcome to Smartprogrammeron.blogspot.in";
String str2 = new String("Welcome to Smartprogrammeron.blogspot.in");
System.out.println("str1.equals(str2) is " + str1.equals(str2));
System.out.print("str1 == str2 is ");
System.out.print(str1==str2);
}
}
However, the == operator checks only whether str1 and str2 refer to the same object; it does not tell you whether they have the same contents. Therefore, you cannot use the == operator to find out whether two string variables have the same contents. Instead, you should use the equals method.
{
public static void main(String[] arg)
{
String str1 = "Welcome to Smartprogrammeron.blogspot.in";
String str2 = new String("Welcome to Smartprogrammeron.blogspot.in");
System.out.println("str1.equals(str2) is " + str1.equals(str2));
System.out.print("str1 == str2 is ");
System.out.print(str1==str2);
}
}
However, the == operator checks only whether str1 and str2 refer to the same object; it does not tell you whether they have the same contents. Therefore, you cannot use the == operator to find out whether two string variables have the same contents. Instead, you should use the equals method.