Home > Java > javaTutorial > What is the code for line break in java

What is the code for line break in java

下次还敢
Release: 2024-04-25 22:51:18
Original
1083 people have browsed it

The newline code in Java is "\n", which can be achieved by using it in a string or using it in the println() method.

What is the code for line break in java

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>
    Copy after login
  • Use in println() method:

    <code class="java">System.out.println("第一行");
    System.out.println("第二行");</code>
    Copy after login

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>
Copy after login

Output result

<code>姓名:张三
年龄:30

姓名:张三
年龄:30</code>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template