System.out.println vs PrintWriter

Is there any difference in using these two? When will you use one over the other?

System.out.println(result); 

OR

  PrintWriter out = new PrintWriter(System.out); out.println(result); out.flush(); 
+10
java printing
source share
3 answers

The difference is that System.out is PrintStream and the other is PrintWriter . PrintStream intended to record a stream of bytes , and PrintWriter intended to record a stream of characters (and, therefore, it deals with character encodings, etc.). For most use cases there is no difference.

+10
source share

System.out is an instance of PrintStream

So your question narrows to PrintStream vs PrintWriter

  • All characters printed by PrintStream are converted to bytes using the platform's default character encoding. (Syso directly writes to the system output / console)

  • The PrintWriter class should be used in situations where you want to write characters, not bytes.

+5
source share

Yes, there is a slight difference. out.println() is short and is used in JSP, while PrintWriter used in servlets. out.println() also inferred from PrintWriter.

0
source share

All Articles