please excuse me if this is an existing stack overflow issue, but I would go through so many threads. But still I can’t understand what they are trying to discuss about two link comparisons of the same class, please help get 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");
}
}
Exit: Different objects of the same class are not equals
Now what I can’t understand is on what grounds the JVM will check these two objects (object1 and object2). And I would override the .equal (), hashCode (), toString () methods in the class A. See here my common 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 an explanation, I am very grateful to them.
vikky source
share