just a try:
public void print(Object obj),from API
Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
System.out.println(Object o); --> System.out.println(String.valueOf(o));
And for String.valueOf(Object o), again from API
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
And now lets try the problem:
1.
String s=null;
System.out.println(s);
here s==null, so the string "null" will be returned by the String.valueOf() method and get translated and printed by the print() method.
2.
System.out.println(t1.toString());
here t1.toString() will be evaluated first and null, of String type(since the return type of toString method is String), is returned and the expression is actually the same as that in case 1, thus "null" is printed without any exceptions.
3.
System.out.println(t1);
here t1 is not null, and thus String.valueOf(t1), t1.toString() is called and the return string is said to be
translated into bytes according to the platform's default character encoding.....err....nullpointer exception arises when attempting to translate.....(but i dun know what's the translatation process is, that's why i put "just a try" at the very beginning)
This is probably the only case that a System.out.println/print () could throw a null pointer exception, the case that a statement tries to print a nonnull object for which a null string is returned by its toString() method.
Could anyone explain more on this issue?
[ June 09, 2003: Message edited by: Yi Meng ]