如果你看看你看到的PrintWriter.println(Object)的Javadoc和源代码
/** * Prints an Object and then terminates the line. This method calls * at first String.valueOf(x) to get the printed object's string value, * then behaves as * though it invokes{@link #print(String)}
and then *{@link #println()}
. * * @param x TheObject
to be printed. */ public void println(Object x) { String s = String.valueOf(x); synchronized (lock) { print(s); println(); } }
反过来,String.valueOf(Object)可以
/** * Returns the string representation of the {@code Object} argument. * * @param obj an {@code Object}. * @return if the argument is {@code null}, then a string equal to * {@code "null"}; otherwise, the value of * {@code obj.toString()} is returned. * @see java.lang.Object#toString() */ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
所以你可以看到toString()
为你而来.
正如文档中所述,您可以假设将来或其他实现中不会更改.即代码可能会更改,但记录的功能将始终保留.
如果你看看你看到的PrintWriter.println(Object)的Javadoc和源代码
/** * Prints an Object and then terminates the line. This method calls * at first String.valueOf(x) to get the printed object's string value, * then behaves as * though it invokes{@link #print(String)}
and then *{@link #println()}
. * * @param x TheObject
to be printed. */ public void println(Object x) { String s = String.valueOf(x); synchronized (lock) { print(s); println(); } }
反过来,String.valueOf(Object)可以
/** * Returns the string representation of the {@code Object} argument. * * @param obj an {@code Object}. * @return if the argument is {@code null}, then a string equal to * {@code "null"}; otherwise, the value of * {@code obj.toString()} is returned. * @see java.lang.Object#toString() */ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
所以你可以看到toString()
为你而来.
正如文档中所述,您可以假设将来或其他实现中不会更改.即代码可能会更改,但记录的功能将始终保留.