Println (String s) vs println (Object o)

It seems to me that PrintStream.print(Object x)both are PrintStream.println(Object x)identical PrintStream.print(String x)and PrintStream.println(String x).

Is there an obvious reason for both? Are they different? API documents readability? Efficiency?

(With autoboxing, I suspect that even printing methods that take primitives as arguments are redundant ... however, these methods precede the autoboxing function to explain.)

+3
source share
2 answers

They do not do the same:

print(Object x)calls String.valueOf(x)that returns:

(obj == null) ? "null" : obj.toString();

So, we have an additional method toString().

, String.toString() this. API .

+10
PrintStream.print(Object x)

,

String.valueOf(Object)

PrintStream.print( x)

, null

+2

Source: https://habr.com/ru/post/1765048/


All Articles