The newline code in Java is "\n", which can be achieved by using it in a string or using it in the println() method.
The code for newline in Java
In Java, the code for newline is\n
, which represents a newline character.
How to use newline codes
In Java, there are two ways to use newline codes:
Use in string:
<code class="java">String str = "第一行\n第二行"; // 打印换行后的字符串 System.out.println(str);</code>
Use in println() method:
<code class="java">System.out.println("第一行"); System.out.println("第二行");</code>
Effect
Regardless of which method is used, the \n
characters will cause the text to wrap in the console.
Example
<code class="java">// 在字符串中使用换行 String str = "姓名:张三\n年龄:30"; System.out.println(str); // 在 println() 方法中使用换行 System.out.println("姓名:" + "张三"); System.out.println("年龄:" + "30");</code>
Output result
<code>姓名:张三 年龄:30 姓名:张三 年龄:30</code>
The above is the detailed content of What is the code for line break in java. For more information, please follow other related articles on the PHP Chinese website!