Java - “\n”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19008970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:19:47  来源:igfitidea点击:

Java - What does "\n" mean?

java

提问by dock_side_tough

I had created a 2-dimensional array in Java and I was looking for a way to print it out on the console so that I could confirm that the stuff I was making was correct. I found some code online that performed this task for me, but I had a question about what a particular bit of the code meant.

我用 Java 创建了一个二维数组,我正在寻找一种方法将它打印在控制台上,以便我可以确认我制作的东西是正确的。我在网上找到了一些为我执行此任务的代码,但我对代码的特定部分含义有疑问。

int n = 10;
int[][] Grid = new int[n][n];

//some code dealing with populating Grid

void PrintGrid() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(Grid[i][j] + " ");
        }
        System.out.print("\n");
    }
}

What does "\n" do? I tried searching on Google, but since it's such a small bit of code I couldn't find much.

"\n" 有什么作用?我尝试在 Google 上搜索,但由于代码太少,我找不到太多。

回答by Suresh Atta

\n

That means a new line is printed.

这意味着打印了一个新行。

As a side note there is no need to write that extra line . There is an built in inbuilt function there.

作为旁注,没有必要写额外的行。那里有一个内置的内置功能。

 println()  //prints the content in new line

Learn more from docs

从文档中了解更多信息

回答by Peter Lawrey

Its is a new line

它是一条新线

Escape Sequences
Escape Sequence Description
\t  Insert a tab in the text at this point.
\b  Insert a backspace in the text at this point.
\n  Insert a newline in the text at this point.
\r  Insert a carriage return in the text at this point.
\f  Insert a formfeed in the text at this point.
\'  Insert a single quote character in the text at this point.
\"  Insert a double quote character in the text at this point.
\  Insert a backslash character in the text at this point.

http://docs.oracle.com/javase/tutorial/java/data/characters.html

http://docs.oracle.com/javase/tutorial/java/data/characters.html

回答by NFE

\n 

This means insert a newline in the text at this point.

这意味着此时在文本中插入一个换行符。

Just example

只是例子

System.out.println("hello\nworld");

Output:

输出:

hello
world

回答by Nunners

\nis an escape character for strings that is replaced with the new line object. Writing \nin a string that prints out will print out a new line instead of the \n

\n是用新行对象替换的字符串的转义字符。\n在打印出的字符串中写入将打印出一个新行而不是\n

Java Escape Characters

Java 转义字符

回答by Maciej Cygan

(as per http://java.sun.com/...ex/Pattern.html)

(根据http://java.sun.com/...ex/Pattern.html

The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\matches a single backslash and { matches a left brace.

反斜杠字符 (' \') 用于引入转义结构,如上表所定义,以及引用否则将被解释为非转义结构的字符。因此,表达式\\匹配单个反斜杠,{ 匹配左大括号。

Other examples of usage :

其他用法示例:

\ The backslash character<br>
\t The tab character ('\u0009')<br>
\n The newline (line feed) character ('\u000A')<br>
\r The carriage-return character ('\u000D')<br>
\f The form-feed character ('\u000C')<br>
\a The alert (bell) character ('\u0007')<br>
\e The escape character ('\u001B')<br>
\cx The control character corresponding to x <br>

回答by nhgrif

In the specific case of the code example from the original question, the

在原始问题的代码示例的特定情况下,

System.out.print("\n");

is there to move to a new line between incrementing i.

是否有移动到递增 i 之间的新行。

So the first print statement prints all of the elements of Grid[0][j]. When the innermost for loop has completed, the "\n" gets printed and then all of the elements of Grid[1][j] are printed on the next line, and this is repeated until you have a 10x10 grid of the elements of the 2-dimensional array, Grid.

所以第一个打印语句打印了 Grid[0][j] 的所有元素。当最里面的 for 循环完成后,“\n”被打印出来,然后 Grid[1][j] 的所有元素都打印在下一行,重复这个过程,直到你有一个 10x10 的网格元素二维数组,网格。

回答by sanjay

\n is add a new line.

\n 是添加一个新行。

Please note java has method System.out.println("Write text here");

请注意 java 有方法 System.out.println("Write text here");

Notice the difference:

注意区别:

Code:

代码:

System.out.println("Text 1");
System.out.println("Text 2");

Output:

输出:

Text 1
Text 2

Code:

代码:

System.out.print("Text 1");
System.out.print("Text 2");

Output:

输出:

Text 1Text 2