The designers of Java decided that they wanted to make it nice and simple to print any object at all, using statements like
System.out.println(something);
System.out.print(something);
someOtherPrintWriter.println(something);
without the programmer having to worry too much about what something
actually was, so they made lots of versions of those methods. But they couldn't anticipate every possible class that someone might want to print an object of.
But because every class extends Object
, either directly or indirectly, all they needed to do was to make any instance of Object
printable - which basically meant providing a way to convert any Object
to a String
.
They did that by including a toString
method in the Object
class, and making print
and println
use it. Then, if anyone writes a class and needs it objects to be printed in a particular way, all they need to do is override toString
, and then print
and println
will both do what the programmer expects.