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.
%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.
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.
\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.
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
\n
This means insert a newline in the text at this point.
Just example
System.out.println("hello\nworld");
Output:
hello
world
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With