| |
|
1.17 Text Screen Output
In Java, textual output to the terminal can be produced by using the following statements:
-
System.out.print(s)
prints String s to the screen without adding a new line. Successive calls will continue printing where the last printed letter stopped.
-
System.out.println(s)
prints String s and adds a new line.
Besides
System.out, there also exists System.err, which refers to the same screen for output, but prints to a different stream. When the output of a command or program is redirected using the >
(greater than) operator, then this refers to output generated by the standard output of a program (i.e. System.out). Messages from the standard error of a program (i.e. System.err) are not affected, and are still printed to the screen without being sent into the redirected output.
System.err is used in a similar fashion as System.out:
-
System.err.print(s);
-
System.err.println(s);
Certain escape characters can be used to further format output from
System.out or System.err, by including them in the printed string:
- \n (backslash n) adds a new line, e.g.
System.out.println("Line 1\nLine2");
- \r (backslash r) returns the cursor to the beginning of the line without adding a new line. This is useful if you would like to give status information for computing something, e.g.
System.out.print("Documents done: " + i + "\r");
|
|