Sample program to test String Equality in Java using equals to (==) operator and String.equals() method.
public class StringEqualsTest {
public static void main(String[] args) {
String str1 = "hello";
String str2 = str1;
String str3 = new String("hellp");
String str4 = new String("hello 2");
String str5 = "hello";
System.out.println("Comparison using == : " + (str1 == str5));
System.out.println("Comparison using == : " + (str1 == str2));
System.out.println("Comparison Using equals() method : " + str1.equals(str2));
System.out.println("Comparison using == : " + (str3 == str4));
System.out.println("Comparison Using equals() method : " + str3.equals(str4));
}
}
Visited 5 times, 1 visit(s) today


