简体   繁体   中英

Difference between System.out and Printstream

Is there any difference in these statements

System.out.println(error);

AND

PrintStream ps = new PrintStream((OutputStream)System.out);
ps.println(error);

System.out is already a PrintStream ,

PrintStream ps = new PrintStream((OutputStream)(System.out));

would only wrap it once more which seems pointless.

There is no difference except for the fact that you made a copy of out .

From JavaDoc

out

public static final PrintStream out

It is already of type PrintStream .

Basically, there is no difference. The second way is too prolonged. out is a static field of type java.io.PrintStream in System class. You could directly use it instead of casting it to Outputstream and again wrapping it inside another PrintStream reference. They both are going to use the same underlying PrintStream object regardless.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM