What is the reason for the unconditional call to toString () in this case? And why does not the unconditional call of the second method occur?

class AnyClass{ private Object obj; public AnyClass(Object cif){ obj = cif; } public String toString(){ return obj.toString(); } public String str(){ return obj.toString(); } } class AnonymClasses { public static void main(String[] args) { AnyClass ac = new AnyClass(new Integer(10)); System.out.println(ac); } } 

    2 answers 2

    The println() system method (output to the output stream, usually to the screen) calls the toString() method whenever it takes an object as an argument, as it is in its code.

    Since an object can have very different implementations, to ensure that it can be guaranteed to be “printed”, the Object class defines the toString() method, which by default displays the name of the object and other related information.

    The println() method cannot know the "look" of an arbitrary object and, in order to display it, refers to the toString() method, which is necessarily present in any object and returns a string that can be printed.

    Even if you do not redefine the toString() method in your class, the Object class method will be used.

    You yourself do not need to call this method, it makes the system printing method to guarantee the expected result.

    Your own methods will not be called by system methods, because they do not know anything about them, whatever you write there.

    • Thank you very much for the clear and detailed response. - Alexander Kochurov

    Just look at the implementation

     public void print(Object obj) { write(String.valueOf(obj)); } 

    Get down below

     public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } 

    Well, like everything.

    toString() is the standard method in the Object class, str is not.

    • Thanks for the answer. That is, if objects of different types are used in a class and standard methods for these types are described in the same class, will these methods be unconditionally applied to objects? If so, what is it for? - Alexander Kochurov
    • one
      @Alexander Kochurov I think you should read about the standard methods of the Object class and about inheritance / polyphorphism in Java . Maybe something from here.stackoverflow.com/questions/416634/… will help you. - pavel
    • I know that all classes are inherited from Object and inherit its methods, and that in this case I override this method, but why it is called when I did not call it is not clear to me. If I create my class, extend AnyClass with it, and redefine its method, it will not be unconditionally invoked as in the case of inheritance from Object. And if I wanted to read the whole book to find the answer, I would have done so. - Alexander Kochurov
    • @AlexanderKochurov You can remove the toString() method from your class altogether; it will still work because there is an Object in the parent class. - Igor Kudryashov
    • I understand it. But why is the overridden method called on its own? - Alexander Kochurov