Java What does "\ n" mean?

I created a two-dimensional array in Java, and I was looking for a way to print it to the console so that I could confirm that the material I was doing was correct. I found code on the Internet that performed this task for me, but I had a question about what a specific bit of code meant.

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 to search on Google, but since this is such a small code code, I could not find much.

+7
java
source share
7 answers

This 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

+34
source share
 \n 

This means that a new line is printed.

As a side note, there is no need to write this extra line. There is a built-in built-in function.

  println() //prints the content in new line 

Learn more from the documentation.

+5
source share
 \n 

This means that at the moment the text of the new line is inserted into the text.

Just an example

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

Output:

 hello world 
+5
source share

(according to http://java.sun.com/...ex/Pattern.html )

The backslash character (' \ ') is used to represent escaped constructions as defined in the table above, as well as to quote characters that would otherwise be interpreted as uninsulated constructions. Thus, the expression \\ matches one backslash and {matches the left bracket.

Other usage examples:

 \\ 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> 
+2
source share

\n is the escape character for strings, which is replaced by a new line object. Writing \n in the line that is displayed will print a new line instead of \n

Java Escape Characters

+1
source share

In the specific case of the sample code from the source question

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

there is a transition to a new line between the increment i.

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

0
source share

\ n adds a new line.

Note that java has a System.out.println method ("Enter text here");

Please note the difference:

the code:

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

Output:

 Text 1 Text 2 

the code:

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

Output:

 Text 1Text 2 
0
source share

All Articles