Why is the toString () method called when an object is printed?

I cannot understand why, when I use the println method for the fourth object, it returns the value of the toString method. I never called the toString method, why am I getting a return value?

public class Main { public static void main(String[] args) { Quarter q = new Quarter(); Nickel n = new Nickel(); System.out.println(q); System.out.println(n); } } public abstract class Money { private int value; public Money(int v) { value=v; } public abstract int getValue(); protected int myValue() { return value; } public abstract String toString(); } public abstract class Coin extends Money { public Coin(int value) { super(value); System.out.println("I am a coin, my value is " + getValue()); } } public class Quarter extends Coin { public Quarter () { super(25); } public int getValue() { return myValue(); } public String toString() { return "A Quarter is "+getValue(); } } public class Nickel extends Coin { public Nickel () { super(5); } public int getValue() { return myValue(); } public String toString() { return "A "+this.getClass().getName()+ " is "+getValue(); } } 
+8
java object
source share
5 answers

On Referring to java docs, what I know is,

When you call the PrintStream class method print (obj) / println (obj) , and inside it calls the write method with an argument like String.valueOf (obj) , shown below:

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

Now String.valueOf (obj) performs the task of calling the String method, as shown below:

  /** * Returns the string representation of the <code>Object</code> argument. * * @param obj an <code>Object</code>. * @return if the argument is <code>null</code>, then a string equal to * <code>"null"</code>; otherwise, the value of * <code>obj.toString()</code> is returned. * @see java.lang.Object#toString() */ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } 
+18
source share

Because PrintStream.println has an overload that takes an Object and then calls its toString method.

+3
source share

Since this function works, it formats primitive types for you, but when you pass it an object, it will call .toString() on it.

If you don't override it, it will output the default implementation .toString() ( Class@somenumber ), which is not very useful ...

+1
source share

When you try to print an object directly, by default it is called by the toString method to override this toString method to print the attributes of your class.

+1
source share

Since all classes in java are subclasses of java.lang.Object, so whenever you try to call the System.out.println () method to print an object, it calls the toString () method of the Object class.

For security reasons, the method prints a hash code, not the values โ€‹โ€‹of this object, but you have inherited this method in your class and expanded its definition to print the values โ€‹โ€‹of objects

 public String toString() { return "A Quarter is "+getValue(); } 

So you get the return value.

0
source share

All Articles