-5

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

Community
  • 1
  • 1
KarthikPon
  • 169
  • 5
  • 10
  • 1
    No strings are being compared in your question, and there is no way to know what the `equals()` method call does without seeing the `Object_Comparison` class. – Robby Cornelissen Sep 18 '19 at 03:38
  • @RobbyCornelissen The class is there, right in front of you. – GhostCat Sep 18 '19 at 03:42
  • 1
    If the class doesn't override `equals(Object)` then the result is identical to using `==` (i.e. reference equality). For classes that do override `equals(Object)` you can look at the source code to see how it's implemented; this can be done via your IDE or you can browse the [online repository](https://hg.openjdk.java.net/jdk). – Slaw Sep 18 '19 at 03:43
  • 1
    @GhostCat I believe Robby is pointing out the fact the question _title_ specifically mentions `String` yet the question _body_ doesn't show any use of `String#equals`. – Slaw Sep 18 '19 at 03:45
  • @Slaw I saw that. But the code in the question itself is clear enough to explain what is going on, imho. – GhostCat Sep 18 '19 at 03:47
  • Unrelated: learn about java naming rules. Variable/field names always go cameCase, and you dont use the "_" character in names, unless for SOME_CONSTANT. – GhostCat Sep 18 '19 at 03:54

2 Answers2

3

Your code doesn't use strings, and as your class Object_Comparison isn't overriding the base equals() 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

Object_Comparison T1 = new Object_Comparison();
Object_Comparison T2 = new Object_Comparison();

with

Object t1 = new Object();
Object t2 ...

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Without any fields in the class, it is unclear how you want `equals` to behave. What would it compare other than object identity? – Thilo Sep 18 '19 at 03:58
  • @Thilo Exactly. Thus I am only explaining what is going with that current code. No point in speculating *what else* the OP has on his mind. – GhostCat Sep 18 '19 at 04:05
  • 1
    Well, people are not always clear in phrasing their questions. Someone who calls an `equals` method (or any other method) should have an idea what that method is supposed to do. Just trying to get OP to explain what he wanted to happen so that we can better understand what the question is about. – Thilo Sep 18 '19 at 04:09
3

I know that .equals methods from the Object class in Java compares the content of the object

Not necessarily, no.

Every class has to implement the logic to "compare the content of the object". There is no built-in generic way in Java that works for all classes.

So while it does compare the contents for things like String or ArrayList<Integer>, that is only because String, ArrayList and Integer have implemented their own versions of the equals method accordingly.

If you do not implement equals in your own class, you will inherit it from the parent class. And if that ends up being Object, then, no, the default implementation provided there does not compare contents: Two objects are only considered equal if they are the same object instance.

And if you do implememt equals, make sure to also implement hashCode(): Why do I need to override the equals and hashCode methods in Java?

Thilo
  • 257,207
  • 101
  • 511
  • 656