As I know == basically compares memory location, So I totally understand that line 1 is returning true. But we have not overridden the equal method in class , then why line 2 is returning true?
private String category = "procedura1";
public static void main(String[] args) {
Lang obj1 = new Lang();
Lang obj2 = new Lang();
if (obj1.category == obj2.category) { /// Line 1
System.out.println("Equal");
} else {
System.out.println("Not equal");
}
if (obj1.category.equals(obj2.category)) { /// Line 2
System.out.println("Equal");
} else {
System.out.println("Not equal");
}
}
And Why line 3 of following returning false?
String a1 = new String("String 1");
String a2 = new String("String 1");
if (a1 == a2) { // Line 3
System.out.println("True");
} else {
System.out.println("False");
}
if (a1.equals(a2)) {
System.out.println("True");
} else {
System.out.println("False");
}