简体   繁体   English

System.out和Printstream之间的区别

[英]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 , System.out已经是一个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 . 目前除了您所做的副本其实没有什么区别out

From JavaDoc 来自JavaDoc

out

public static final PrintStream out public static final PrintStream out

It is already of type PrintStream . 它已经是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. out是System类中java.io.PrintStream类型的静态字段。 You could directly use it instead of casting it to Outputstream and again wrapping it inside another PrintStream reference. 您可以直接使用它而不是将其转换为Outputstream并再次将其包装在另一个PrintStream引用中。 They both are going to use the same underlying PrintStream object regardless. 无论如何,它们都将使用相同的底层PrintStream对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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