Java has two ways of wrapping lines: System.out.println() method automatically wraps lines; \n escape characters manually wrap lines. It is usually recommended to use System.out.println() method.
Two methods of line wrapping in Java
In Java, you can use the following two methods to output results Newline:
1. System.out.println() method
The System.out.println() method automatically adds a newline character at the end of the output. So just use System.out.println()
to achieve line wrapping.
<code class="java">System.out.println("第一行"); System.out.println("第二行");</code>
Output:
<code>第一行 第二行</code>
2. \n Escape character
\n Escape character represents newline character. Adding \n to the output string inserts a newline character at that position.
<code class="java">System.out.print("第一行\n"); System.out.print("第二行");</code>
Output:
<code>第一行 第二行</code>
It should be noted that the System.out.print() method does not automatically add newlines, so you need to add them manually.
Both methods can achieve line wrapping, but it is generally recommended to use the System.out.println()
method because it is more concise and less error-prone.
The above is the detailed content of How to wrap output results in java. For more information, please follow other related articles on the PHP Chinese website!