17

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.

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.

dock_side_tough
  • 355
  • 2
  • 5
  • 11
  • 23
    Did you really searched [using your title](https://www.google.co.in/search?q=What+is+%5Cn+in+Java&rlz=2C1CHWA_enIN0536IN0536&oq=What+is+%5Cn+in+Java&aqs=chrome..69i57j0l5.3002j0&sourceid=chrome&espvd=210&es_sm=122&ie=UTF-8)? – Rohit Jain Sep 25 '13 at 15:25
  • Although I should said this is not bring much value on StackOverflow with this kind of question, but then we got all simple questions in one place anyway. – Casper Ngo Sep 25 '13 at 15:29
  • 1
    Did you try to remove the line (put it in comment) and see what the difference was in the output? – Michaël Benjamin Saerens Sep 25 '13 at 15:30
  • 1
    I don't understand why this kind of questions gets too many answers. I mean, it's too simple if just one person answers is enough. Eight answers and counting... – dic19 Sep 25 '13 at 15:32
  • Yeah... I messed up. I Google searched "/n" instead of "\n". Thanks for the responses anyway. – dock_side_tough Sep 25 '13 at 15:33
  • @RohitJain - Problem is, if he just Googles for `"\n"` he gets only one relevant answer in the first 50 hits, and that could be easily overlooked. The odds are better if you throw in "Java". – Hot Licks Sep 25 '13 at 15:34

7 Answers7

44

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

From the Java Language Specification.

EscapeSequence:
\ b (backspace BS, Unicode \u0008)
\ t (horizontal tab HT, Unicode \u0009)
\ n (linefeed LF, Unicode \u000a)
\ f (form feed FF, Unicode \u000c)
\ r (carriage return CR, Unicode \u000d)
\ " (double quote ", Unicode \u0022)
\ ' (single quote ', Unicode \u0027)
\ \ (backslash \, Unicode \u005c)

https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    And note that in Java these escape sequences are valid in literal strings surrounded by "double quotes" and in individual characters literals surrounded by 'single quotes'. That is, `'\n'` is one character (numerically 0x0A), even though you type it as two characters. – Hot Licks Sep 25 '13 at 15:29
  • I was wondering, in Linux shell why `\n` and `\\n` both give a new line, where `\\\n` gives literally `\n`? how does the shell deal with that and what happens behind the scenes? – Mohammed Noureldin Sep 12 '17 at 23:54
  • @MohammedNoureldin the \\n should be back slash + `n` and \\\n should be back slash + new line. – Peter Lawrey Sep 13 '17 at 05:53
  • 1
    Is there any documentation on what `\n` ***is***? *"Insert a newline in the text at this point"* Which Unicode character is *"newline"*? U+000D? Nope, that's `CARRIAGE RETURN` U+000A? Nope, that's [`LINE FEED`](https://www.fileformat.info/info/unicode/char/000a/index.htm). Does it insert U+000D? U+000A? U+000D+U+000A? U+0085? U+2028? U+2029? Is it an implementation detail that is up to the JRE you're running on? Does it change depending if you're running on Windows, Mac, or Unix? – Ian Boyd Mar 05 '22 at 16:59
  • @IanBoyd It's a line feed, but I can't find anything official which says that. https://stackoverflow.com/questions/9260126/what-are-the-differences-between-char-literals-n-and-r-in-java – Peter Lawrey Mar 07 '22 at 11:49
  • 1
    @IanBoyd Actually there is this. https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6 – Peter Lawrey Mar 07 '22 at 11:50
8
\n 

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

Just example

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

Output:

hello
world
NFE
  • 1,147
  • 1
  • 9
  • 22
5
\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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
5

(as per 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>
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72
1

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

Java Escape Characters

Community
  • 1
  • 1
Nunners
  • 3,047
  • 13
  • 17
1

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.

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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

\n is add a new line.

Please note java has method 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
sanjay
  • 354
  • 2
  • 6
  • 27