public class Counter {
String s1;
Counter(String s) {
this.s1 = s;
}
public static void main(String[] args) {
Counter t1 = new Counter("test");
Counter t2 = new Counter("test");
System.out.println(t1 == t2); //false - Output
System.out.println(t1.equals(t2)); //false - Output
String s1 = new String("rt");
String s2 = new String("rt");
System.out.println(s1 == s2); //false - Output
System.out.println(s1.equals(s2)); //true - Output
}
}
Reason: Getting different output for equals method.
Since java.lang.String class override equals method, It return true if two String object contains same content but == will only return true if two references are pointing to same object.
For == operator I am getting proper result but for equals method I am bit confused in my program as the output is different. Please, can anyone share their views on this? thanks in advance