could someone explain the source code of toString method which is overridden in the String class?

Viewed 414

I have few questions it would be helpful if someone answered it.

  1. What does the toString() method returns in the following code?

    /** 
    * This object (which is already a string!) is itself returned. 
    * 
    * @return the string itself.
    */ 
    public String toString() {
    return this; 
    }
    

As you can see the above method returns this which means the reference of the object on which it is called is returned, then if we print a String object, automatically the toString() method is called and the reference is returned to println() method ,but we are getting the output as value(Mango) not the reference (let's say the memory address would be dtt22723836@dtf).

If this (reference) is returned, then the output should be the following

String a="Mango";
System.out.println(a);
/* Actual output : Mango
My assumption :dtt22723836@dtf (as the reference is returned from toString() 
to println()
 */
  1. what is stored in the reference variable of a String? value(Mango) or the reference(dtt22723836@dtf)?

correct me if I'm wrong. Thank you.

3 Answers

You seem to be looking at it from a strange angle. There's one fact that you need to start with: System.out.println() knows how to show the actual text content of a String object. This means that when you give it a String, it will print the text of a string ("Mango", and not something like java.lang.String@ab2323). It's only when you give it a non-String Object that it calls toString() method (via String.valueOf() - although valueOf seems to be called even in println(String), supposedly to deal with nulls too).

The text "dtt22723836@dtf" is usually the String object created by the toString() method implemented in java.lang.Object (and not overridden in subclasses) - println() calls this method to get the string to print.

Now, String a = "Mango" is already a String, meaning that System.out.println(a) knows how to print the text content, which is Mango rather than any fabrication from java.lang.Object.toString() default implementation.

This is why java.lang.String.toString() does nothing but return the object on which the toString() method is called. It's already a String object, when it is returned, it's used by println() in predictable ways.

java.lang.String.toString() has nothing to compute, the object itself is a String. The End.

The toString() method is specified to return a String, so the instance of String just returns itself. (It could create a new instance, but that would still be a String).

On the other end, the println(String) method (from PrintStream) ends up calling the getChars() method of String to print that characters. The println(String) method is called if the argument is a String, for other reference types println(Object) method is called, This the one is the one that uses toString to get the textual representation of its argument.

The toString method gets called inside the println method.

The first line in println(Object x):

String s = String.valueOf(x);

And String.valueOf(Object obj) calls:

return (obj == null) ? "null" : obj.toString();

Note: If the class doesn't override the toString method NOT the address will get printed but an unsigned hexadecimal representation of the hash code of the object combined with the class name an an @.

Related