+ 2

Hello all technicien friends! Could you tell me what is the second printed line?the location adress of a1? Thanks👍

https://code.sololearn.com/c3RpLs53mGMn/?ref=app

9th Jan 2021, 9:08 AM
Theo Martier
Theo Martier - avatar
6 ответов
+ 2
Hi Theo Martier- 🇧🇪Belgium , Your second line print the address of a2 reference. And whenever you are trying to print the reference of any class object then the compiler will check. Is this class override the toString() method or not?. If yes then your toString() method will be call if not then then it will call object class toString () method. In your case you didn't override toString () in your class. So for now Object class to String method will be call when you will print the reference of the object. And the definition of toString() method of Object class is down below. public String toString() { return getClass().getName()+"@"+Integer.toHexString(hashCode()); } As per the implementation of toString() method of Object class. You are getting output like Animal@34ba273 something like this. 🙂 Hope you will be clear. 🐾
9th Jan 2021, 7:50 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 1
difference between == and equal is following: == symbol compares 2 object references since equal compares ovject values. for example Integer a=new Integer(5) Integer b=new Integer(5) integer c=a. a not == b but equal b a == c since both variables link the same object
9th Jan 2021, 9:27 AM
george
george - avatar
+ 1
in your example after asign a1=a2 add aditional steing on printing a1==a2 it will be true
9th Jan 2021, 9:29 AM
george
george - avatar
+ 1
Ou sorry,:) accordingto your real question Animal@ is a reference name of object or with other words unique identification of class. Each time when object created (new operator), special unique identificator generated(className@randomnum)and assign to the object. So when you make comparison via a==b with help of this unique name comparison detect equality.
9th Jan 2021, 11:31 AM
george
george - avatar
0
Sorry george, but You have not answered to my question :what is the second printed line ( Animal@36.....)?
9th Jan 2021, 9:56 AM
Theo Martier
Theo Martier - avatar
0
Thank you george and Pawan i think it's clearer! To resume it is an adress and the a2's address ( and not a1) in this particular instruction
9th Jan 2021, 9:14 PM
Theo Martier
Theo Martier - avatar