Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, February 2, 2016

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

Why is the toString() method being called when I print an 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();      }  }  

Answer by Oliver Charlesworth for Why is the toString() method being called when I print an object?


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

Answer by fge for Why is the toString() method being called when I print an object?


Because this is how this function operates: it formats the 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 .toString() implementation (Class@somenumber) which is not really useful...

Answer by ues for Why is the toString() method being called when I print an object?


When you are directly trying to print an object, by default it will call the toString method you need to override that toString method to print the attributes of your class.

Answer by Prateek for Why is the toString() method being called when I print an object?


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 Object argument.   *   * @param   obj   an Object.   * @return  if the argument is null, then a string equal to   *          "null"; otherwise, the value of   *          obj.toString() is returned.   * @see     java.lang.Object#toString()   */  public static String valueOf(Object obj) {  return (obj == null) ? "null" : obj.toString();  }  

Answer by mrxsunil for Why is the toString() method being called when I print an object?


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

For Security Reasons the method prints a hashcode, not the values of that object, but you have inherited that method in your class and extended its definition to print object values

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

So you get a return value.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.