-2

When printing a reference of any other type than a String, the output is the name of the class along with the HashCode / the string returned by its toString() method. But when a reference of a String is printed, the actual "string values" is shown, is toString() called in this case ? enter image description here

JDoe
  • 96
  • 2
  • 10
  • 1
    Yes and if your class only has one String field it will be handled as a String. – Murat Karagöz Jul 28 '17 at 14:09
  • bcoz `String` overrides `toString()` – Ramanlfc Jul 28 '17 at 14:10
  • can you show code or examples of the output please? – ja08prat Jul 28 '17 at 14:10
  • Check the `toString()` method on the Object class, which has a different implementation of the `toString()` method on the String class. – toongeorges Jul 28 '17 at 14:15
  • System is a class, with a static field out, of type PrintStream. So you're calling the println(Object) method of a PrintStream. – sForSujit Jul 28 '17 at 14:19
  • @pshemo wrong duplicate, IMO. The person is not asking why they get the `class@hash` format, but whether `println` calls the `toString` method. – RealSkeptic Jul 28 '17 at 14:20
  • `out` in `System` class holds `PrintStream` and that class has `println(String x)` to print strings without need to `toString` call. But `PrintStream` also has `println(Object o)` method to handle printing objects from other classes (with help of `String.valueOf` which internally is using `toString`). – Pshemo Jul 28 '17 at 14:22
  • @RealSkeptic Yes I know. I was meant to add this as *additional* duplicate but apparently original duplicate target was from flag, not close-vote so it disappeared. I am looking for better duplicate now. – Pshemo Jul 28 '17 at 14:24
  • Related: [toString() method within System.out.println() a double call?](https://stackoverflow.com/questions/16570937/tostring-method-within-system-out-println-a-double-call) – Pshemo Jul 28 '17 at 14:31

1 Answers1

2

No. If you look at the source code for println(String), you'll see:

public void println(String x) {
    synchronized (this) {
        print(x);
        newLine();
    }
}

The only difference between this and the generic Object-signature version is that String.valueOf is not called:

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

and String.valueOf is the method which contains the call to toString:

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

You should get used to browsing the source files of the JDK if you're curious about details such as this.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • Exact duplicate of https://stackoverflow.com/questions/17051481/how-an-object-will-call-tostring-method-implicitly – sForSujit Jul 28 '17 at 14:35
  • @sForSujit Isn't it subtly different? That question is "how is `toString` called for `Object`s?" and this question is "*is* `toString` called for `String`s?" – Michael Jul 28 '17 at 14:36
  • I am confused. If toString() is not called, then how is the string value getting displayed ? – JDoe Jul 28 '17 at 14:39
  • @shOm A string is a sequence of characters. That's the *only* thing you *can* print. When you try to print an object, it implicitly converts that object to a sequence of characters so it can print it. When that object is already a sequence of characters, it doesn't need to do anything. – Michael Jul 28 '17 at 14:41
  • @Michael, I followed what you said, but, we do not pass the actual objects of Strings to the println() but their references are passed, isn't it ? – JDoe Jul 28 '17 at 14:52
  • At some levels deeper, the getChars method of String gets called to get the actual string data. – ths Jul 28 '17 at 15:40
  • @ShOm "but, we do not pass the actual objects of Strings to the println() but their references are passed, isn't it" yes, but what exactly is confusing you here? JVM via that reference is able to access object it represents so if we have `String s = "foo";` then `s` holds reference to `"foo"` object. But we can write `s.charAt(0)` and JVM will understand that we want to get character at index `0` from object which `s` holds reference to. – Pshemo Jul 28 '17 at 15:53