Look at the source code of print(Object obj)
method from PrintSteam
class:
public void print(Object obj)
{
write(String.valueOf(obj));
}
and valueOf(Object obj)
of String
class:
public static String valueOf(Object obj)
{
return (obj == null) ? "null" : obj.toString();
}
As you see, obj.toString()
is invoked, and as @Guido García states, overriding toString()
method is what you need.
The default implementation of toString()
method in Object
class is as follows:
public String toString()
{
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}