please apologize me if it is already existing question in stack overflow, but I would go through so many threads of stack overflow. But still I am unable to understand what they are try to discuss about two references comparison of the same class, please help to come out of this problem. This is my actual analysis
public class A {
public static void main(String[] args) {
A object1 = new A();
A object2 = new A();
if (object1 == object2)
System.out.println("Different objects of the same class are equals");
else
System.out.println("Different objects of the same class are not equals");
}
}
Output : Different objects of the same class are not equals
Now what I am unable to understanding thing is on which bases JVM will check these two objects (object1 and object2). And I would override .equal(), hashCode(), toString() methods in A
class. Please see here my total code.
public class A {
@Override
public int hashCode() {
return 2000;
}
@Override
public String toString() {
return "12345";
}
public static void main(String[] args) {
A object1 = new A();
A object2 = new A();
if (object1 == object2)
System.out.println("Different objects of the same class are equals");
else
System.out.println("Different objects of the same class are not equals");
}
}
Please give me clear cut explanation, I am very very thankful to them.