I know that .equals methods from the Object class in Java compares the content of the object but what does it do in the below code and why is it FALSE as i put in the comment line? Please let me know.
public class Object_Comparison {
public static void main(String[] args) {
Object_Comparison T1 = new Object_Comparison();
Object_Comparison T2 = new Object_Comparison();
System.out.println("T1-->"+T1);
System.out.println("T2-->"+T2);
System.out.println("T1-->"+T1.hashCode());
System.out.println("T2-->"+T2.hashCode());
System.out.println(T1==T2);
System.out.println(T1.equals(T2)); // what does it contain to do and produces the result as false?
}
}
O/p:
T1-->Object_Comparison@1db9742
T2-->Object_Comparison@106d69c
T1-->31168322
T2-->17225372
false
false
Your code doesn't use strings, and as your class
Object_Comparisonisn't overriding the baseequals()method from java.lang.Object, therefore expected happens.Your code creates two different object T1 and T2. If you compare the "references" to those objects, they are different (T1 != T2). And as the base implementation for
equals()... simply does the same check, that method returns false as well.Given your current code, you could as well replace
with
and get to the exact same results. Because nothing you are showing in your example code does anything different then what you "inherit" from java.lang.Object.