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

What is the code for line break in java

下次还敢
下次还敢Original
2024-04-25 22:51:181033browse

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>
  • 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!

Statement:
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
Previous article:What is a class in javaNext article:What is a class in java