- We use String compare using equels() methods. Its compare both String's contents.
- In case using = = then check both String object's memory address.
- If you know more info about, how do handling String object in JVM? More...
class StringCompare
{
public static void main(String[] arg)
{
System.out.println("String comparition difference between == and equals()");
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
String str4 = new String("hello");
System.out.println("str1 : "+str1);
System.out.println("str2 : "+str2);
System.out.println("str3 : "+str3);
System.out.println("str4 : "+str4);
System.out.println("==");
System.out.print("str1 == str2 : ");
System.out.println(str1 == str2);
System.out.print("str1 == str3 : ");
System.out.println(str1 == str3);
System.out.print("str3 == str4 : ");
System.out.println(str3 == str4);
System.out.println("equals()");
System.out.print("str1.equals(str2) : ");
System.out.println(str1.equals(str2));
System.out.print("str1.equals(str3) : ");
System.out.println(str1.equals(str3));
System.out.print("str3.equals(str4) : ");
System.out.println(str3.equals(str4));
}
}
Output:
No comments:
Post a Comment