Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - What does "\n" mean? [duplicate]

Tags:

java

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.

like image 714
dock_side_tough Avatar asked Sep 25 '13 15:09

dock_side_tough


People also ask

Is \n and %n the same Java?

%n is portable between various platforms, the value emitted from %n will suit the underlying platform, whereas value emitted by \n is same for all the platforms. \n is the correct newline character for Unix-based systems, other systems may use different characters to represent the end of a line.

What does \n mean in code?

With early computers, an ASCII code was created to represent a new line because all text was on one line. In programming languages, such as C, Java, and Perl, the newline character is represented as a '\n' which is an escape sequence.

What do \n and \t do in Java?

\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.


2 Answers

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

like image 127
Peter Lawrey Avatar answered Sep 20 '22 13:09

Peter Lawrey


\n 

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

Just example

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

Output:

hello
world
like image 30
NFE Avatar answered Sep 21 '22 13:09

NFE