Java Reference
In-Depth Information
Therefore, when \n appears in a string in an output statement, it moves the insertion
point to the beginning of the next line on the output device. This explains why Hello
and there. are printed on separate lines.
In Java, \ is called the escape character and \n is called the newline escape sequence.
Let's take a closer look at the newline character, '\n' . Consider the following Java statements:
System.out.print("Hello there. ");
System.out.print("My name is James.");
If these statements are executed in sequence, the output is:
Hello there. My name is James.
Consider the following Java statements:
System.out.print("Hello there.\n");
System.out.print("My name is James.");
The output of these Java statements is:
Hello there.
My name is James.
When \n is encountered in the string, the insertion point is positioned at the beginning
of the next line. Note also that \n may appear anywhere in the string. For example, the
output of the statement:
System.out.print("Hello \nthere. \nMy name is James.");
is:
Hello
there.
My name is James.
Also, note that the output of the statement:
System.out.print("\n");
is the same as the output of the statement:
System.out.println();
Thus, the output of the sequence of statements:
System.out.print("Hello there.\n");
System.out.print("My name is James.");
is equivalent to the output of the sequence of statements:
System.out.println("Hello there.");
System.out.print("My name is James.");
 
Search WWH ::




Custom Search