这是我查的源码
- /**
- * Prints an object. The string produced by the <code>{@link
- * java.lang.String#valueOf(Object)}</code> method is translated
- into bytes
- * according to the platform's default character encoding, and
- these bytes
- * are written in exactly the manner of the <code>{@link #write
- (int)}</code>
- * method.
- *
- * @param obj The <code>Object</code> to be printed
- * @see java.lang.Object#toString()
- */
- public void print(Object obj) {
- write(String.valueOf(obj));
- }
-
- /**
- * Returns the string representation of the <code>Object</code>
- argument.
- *
- * @param obj an <code>Object</code>.
- * @return if the argument is <code>null</code>, then a string
- equal to
- * <code>"null"</code>; otherwise, the value of
- * <code>obj.toString()</code> is returned.
- * @see java.lang.Object#toString()
- */
- public static String valueOf(Object obj) {
- return (obj == null) ? "null" : obj.toString();
- }
复制代码
解释如下
System.out为PrintStream流,他的print(Object obj)方法调用String的valueOf
(Object obj)方法,String的valueOf方法判断如果obj为空的话,返回“nul”,否则返
回obj.toString()
这下你可明白 |