Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The connection between 'System.out.println()' and 'toString()' in Java

What is the connection between System.out.println() and toString() in Java? e.g:

public class A {
    String x = "abc";

    public String toString() {
        return x;
    }
}

public class ADemo {
    public static void main(String[] args) {
        A obj = new A();
        System.out.println(obj);
    }
}

If main class runs, it gives an output as "abc". When I remove the code which overrides toString(), it gives an output as "A@659e0bfd". So, can anyone explain what is the working principle of System.out.println() when I pass the obj object reference as an argument to it? Is it fully connected with toString() method?

like image 518
anisotropic Avatar asked Mar 28 '15 15:03

anisotropic


People also ask

Does system out Println automatically call toString?

System. out. println() tends to call toString() when Object are passed. That's how it prints relatively meaningful output.

Does Println use toString?

By default, the toString() method is called by println, but the method can also be called explicitly on any object. The method can be called on an object, like this – object.

What is the purpose of toString () method in Java?

The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler.

What's the difference between system out print () and system out Println ()?

What is the difference between println() and print()? The main difference between the two is that print() retains the cursor in the same line after printing the argument, while println() moves the cursor to the next line.


2 Answers

System.out is a PrintStream. Printstream defines several versions of the println() function to handle numbers, strings, and so on. When you call PrintStream.println() with an arbitrary object as a parameter, you get the version of the function that acts on an Object. This version of the function

...calls at first String.valueOf(x) to get the printed object's string value...

Looking at String.valueOf(Object), we see that it returns

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

So, long story short, System.out.println(someObject) calls that object's toString() function to convert the object to a string representation.

If your object defines its own toString() function, then that is what will be called. If you don't provide such a function, then your object will inherit toString() from one of its parent classes. In the worst case, it will inherit Object.toString(). That version of toString() is defined to return

a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

Or, in other words:

getClass().getName() + '@' + Integer.toHexString(hashCode())

So, when you call System.out.println() on an object that doesn't define its own version of toString(), you might get the Object version which looks like "classname@someHexNumber".

like image 101
Kenster Avatar answered Sep 18 '22 13:09

Kenster


toString() is a method that exist in the Object class (Root of the inheritence tree) for all classes.

System.out.print() (SOP) will call the toString method when fed an object.

If you don't overwrite the method toString(), SOP will call the parent toString() which, if parent is the Object class, it will print the hashCode of the object

If you overwrite the method, SOP will call your toString() method

like image 31
CMPS Avatar answered Sep 19 '22 13:09

CMPS