If I am printing an object of the class then it is printing the toString()
method implementation even I am not writing the toString()
method so what are the implementation,how it is calling toString()
internally?

- 94,125
- 30
- 188
- 241

- 121
- 1
- 1
- 4
-
1What do you mean by *printing the `toString()` method implementation*? – Maroun Jun 11 '13 at 18:47
5 Answers
You're not explicitly calling toString()
, but implicitly you are:
See:
System.out.println(foo); // foo is a non primitive variable
System
is a class, with a static
field out
, of type PrintStream
. So you're calling the println(Object)
method of a PrintStream
.
It is implemented like this:
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
As we see, it's calling the String.valueOf(Object)
method.
This is implemented as follows:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
And here you see, that toString()
is called.

- 1,265
- 1
- 18
- 31

- 37,490
- 6
- 58
- 83
Every object in Java IS-A(n) Object
as well. Hence, if a toString()
implementation has not been provided by a class the default Object.toString()
gets invoked automatically.
Object.toString()
's default implementation simply prints the object's class name followed by the object's hash code which isn't very helpful. So, one should usually override toString()
to provide a more meaningful String representation of an object's runtime state.
even I am not writing the toString() method so what are the implementation,how it is calling toString() internally?
toString()
is one of the few methods (like equals()
, hashCode()
etc.) that gets called implicitly under certain programmatic situations like (just naming a few)
- printing an object using
println()
- printing a
Collection
of objects (toString()
is invoked on all the elements) - concatenation with a String (like
strObj = "My obj as string is " + myObj;
)

- 51,095
- 9
- 76
- 89
Everything inherits from Object, so the toString on Object will be called if you have not defined one.

- 46,453
- 60
- 198
- 311
-
And Type :). http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Type.html – greedybuddha Jun 11 '13 at 18:50
-
A method will be only called when we call it explicitly ,but here if we are printing an object ,toString()is called how it is possible? – user2475808 Jun 11 '13 at 19:01
-
@MarounMaroun You mean Primitives type not inherited from Object class? Is Object class have any primitive type? – Asif Mushtaq Sep 23 '15 at 17:41
-
@AsifMushtaq Exactly. `Object` class doesn't have primitive types, but does have wrappers - Look for `Integer`, `Double`.. – Maroun Sep 23 '15 at 18:05
toString() method is present in Object class, so when u put obj in System.out.println(obj);, impliciyly it will call toString() present in Object class since every user created class will implicitly inherits Object class so as ur newly created class, that means that toString() is available in ur class so it will print something like for example: "PkgNamePackage.Classname@12cf4" However if u explicitely override toString method and give ur own implementation then it will written the string what ever u give in Overriden tostring method(); ex:
public class DogArray {
@Override
public String toString() {
return "Im the newly created Object";
}
public static void main(String args[]) {
DogArray d1 = new DogArray();
System.out.println(d1);
}
}
output: Im the newly created Object

- 9,998
- 4
- 42
- 62

- 1
- 1
In java object class is super class to the each and every class.whenever your passing parameter to the system.out.println internally object class to string method will be excuted.it returns class name@reference value given but as per our application requirement object class to string method will override in collection and string class.it returns their content.