Difference between System.out and Printstream

前端 未结 3 1115
傲寒
傲寒 2020-12-10 17:28

Is there any difference in these statements

System.out.println(error);

AND

PrintStream ps = new PrintStream((OutputStream)S         


        
相关标签:
3条回答
  • 2020-12-10 17:49

    System.out is already a PrintStream,

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

    would only wrap it once more which seems pointless.

    0 讨论(0)
  • 2020-12-10 17:59

    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.

    0 讨论(0)
  • 2020-12-10 18:01

    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.

    0 讨论(0)
提交回复
热议问题