0
Explain why this?
I did'nt call "tostring" method but it is called itself . Or what it means that a method is called implicitly. https://code.sololearn.com/cuShB763KVix/?ref=app
3 Respostas
0
All objects have method toString()
It is invoked when you print the object.
If you do not define this method in you class Object.toString() is invoked, which prints string consisting of className and hashCode of object.
In your class you override Object.toString().
So when you print object of your class Time toString() is invoked by println.
0
It's a super class ( Object ) method that every class inherits.
It is called when you try to print Object of a class.
0
On Refering to java docs what I understand is that:
When you call PrintStream class print(obj) or println(obj) method then internally it calls write method with argument as String.valueOf(obj) shown below:
public void print(Object obj) {
write(String.valueOf(obj));
}
Now String.valueOf(obj) does the task of calling toString() method as shown below:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}