Why Is the Tostring() Method Being Called When I Print an Object

How toString() is automatically call inside println()

Short answer: public frog1.toString() method call is used inside System.out.println(Object x) call stack.

But how? Let's find it out together :)

Take a look at the PrintStream class (which instance is used as System.out field value by default) source code and its println implementation that accepts Object argument:

public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}

And the String's valueOf method for argument with Object type is:

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

obj is frog1 in your case, its toString() method is called and returns "Hello" String instance for the console output)

Why print statement calls **toString** method in java?

println is overloaded for various types, the one you are invoking is:

java.io.PrintStream.println(java.lang.Object)

Which looks like this:

public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}

And String.valueOf looks like this:

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

So you can see it calls toString() on your object. QED

When is toString() method called

You're calling System.out.println(test), which is a call to PrintStream.println(Object). That will call toString() on the reference you pass in, via String.valueOf:

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

when println() is displaying an Object, which one of the Objects methods does println() call?

When using the signature println(Object) the toString() method is being used to output the string representation of that object.

When toString() is called on an object you've defined it will cascade up the hierarchy of superclasses until reaching java.lang.Object given that there is no overridden toString() method in any intermediate superclasses or the initial class. Upon reaching java.lang.Object the system will call the base toString() method which prints the String literal representation of that Object and is the reason why you will see some memory address in the output.

Why is it common practice to override the method toString() in java to print the instance variables of the class?

We could. But how will other classes, that know absolutely nothing about your subclasses with your myNewToString method, know how to print a string that textually represents, in a concise but informative way, arbitrary subclasses?

The toString method was designed to be overridden to do that. Yes, it does have default behavior but it's not very useful. Its authors wanted you to override it. Overriding it to return what's commonly practiced is more useful, but you don't have to do that. A toString method for an EmailAddress class can return

public String toString() {
return "EmailAddress{localPart = " + localPart + ", domainName = " + domainName + "}";
}

but it's usually more useful to return something like

public String toString() {
return localPart + "@" + domainName;
}

Problem with printing - Inheritance, Polymorphism and toString

Just remove method call super.toString() from toString() method in both classes.
When you make that call you are calling toString() method from parent class. So when it is called from NamedCar class, it calls toString() method from Car class and when it is called from Car class it calls toString() from object class.



Related Topics



Leave a reply



Submit