Explain why this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

7th Mar 2020, 11:29 AM
Muhammad Bilal
Muhammad Bilal - avatar
3 Answers
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.
7th Mar 2020, 11:43 AM
andriy kan
andriy kan - avatar
0
It's a super class ( Object ) method that every class inherits. It is called when you try to print Object of a class.
7th Mar 2020, 12:07 PM
Vishal
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(); }
7th Mar 2020, 1:07 PM
ROBIN C R
ROBIN C R - avatar