I know the println
method of the System.out
object can be used to print any object, regardless of which class it belongs to. But can anyone tell me the mechanism that println
uses to accomplish this task?
Calling System.out.println
on an object uses Object
's toString()
method - either the default implementation in Object
class, or the implementations of classes that override this default.
System.out.println, eventually uses one of PrintStream
's various println
methods, since System.out
is a PrintStream
instance.
If you pass a String
to System.out.println
, it will call println(String)
. If you pass a char[]
to it, it will call println(char[])
. If you pass any other type of Object
, it will call println(Object)
(unless the compiler decides to convert the Object
to a String
, using the object's toString()
method, and then call println(String)
instead).
println(Object x)
converts the Object
to a String
by calling String.valueOf()
, which returns either "null" (for null Objects) or obj.toString()
.
Either way, the Object
's toString()
method is used, unless the Object
is null.
There is very little difference between the way PrintStream/PrintWriter.println(...)
treats String
's and the way the compiler treats String
's. The print streams will automatically convert an Object
to a String
via the toString()
method unless that object is null
(in which case it prints null
). The difference is that you cannot set a String
object to a general Object
--but you can concatenate a String
object with a general purpose Object
which will be concatenated via the toString()
method (unless the Object
is null
). Frankly, this is normallly how you would use the toString()
method. Here is a sample program that illustrates the differences:
public class PrintStreamTester {
public static void main(String... args) {
final MyObject o1 = null;
final MyObject o2 = new MyObject("blah blah blah");
String s1 = "o1.toString() failed";
try {
s1 = o1.toString();
} catch (NullPointerException npe) {
// who cares
}
final String s2 = "" + o1;
final String s3 = o2.toString();
final String s4 = "" + o2;
System.out.println("o1 by itself (below):");
System.out.println(o1);
System.out.println("o1's toString(): " + s1);
System.out.println("o1's \"\" + o1: " + s2);
System.out.println("o2 by itself (below):");
System.out.println(o2);
System.out.println("o2's toString(): " + s3);
System.out.println("o2's \"\" + o1: " + s4);
}
}
class MyObject {
private final String string;
public MyObject(final String string) {
this.string = string;
}
@Override
public String toString() {
return string;
}
}
Here is the output from this program:
o1 by itself (below):
null
o1's toString(): o1.toString() failed
o1's "" + o1: null
o2 by itself (below):
blah blah blah
o2's toString(): blah blah blah
o2's "" + o1: blah blah blah
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.