Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the toString() method being called when I print an object?

Tags:

java

object

I can't seem to understand why when I use println method on the quarter object, it returns the value of the toString method. I never called the toString method why am I getting the 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();
    }
}
like image 212
Nicholas Kong Avatar asked Dec 18 '11 23:12

Nicholas Kong


People also ask

Why is toString automatically called?

The implementation of the method println makes a call to String. valueOf(Object) method. Within the valueOf method, toString is called on the object passed as an argument to the valueOf method. Thus, toString method gets called automatically.

Who invokes the toString method when object is printed?

It contains the toString method. 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.

When toString () method is invoked on an object what is returned?

The toString() method returns the String representation of the object.

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.


1 Answers

On Refering to java docs what i undestand is that,

When you call PrintStream class print(obj) / println(obj) method then internally it called write method with arguement as String.valueOf(obj) shown below :

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

Now String.valueOf(obj) does the task of calling to 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();
}
like image 105
Prateek Avatar answered Sep 29 '22 22:09

Prateek