-2

I was just looking though some code and I came across this line:

stringBuffer.append("\n");

can anyone tell me what it means? Or more specifically what the "\n" part does?

Thanks

Airglow
  • 71
  • 6
  • 1
    It appends (adds to the end of, in this case, the string contained in stringBuffer) a `new line` character. You may need to go over a basic tutorial, it would be in there. – AntonH Apr 10 '14 at 20:15
  • So the \n means new line basically? Or tells the stringBuffer to go to the next line? – Airglow Apr 10 '14 at 20:15
  • 1
    Yes, the `"\n"` means new line. It's easier to remember, rather than the ASCII code for adding a new line. – AntonH Apr 10 '14 at 20:16
  • 1
    I would prefer `System.lineseparator()` in such situations where you are building a String. `\n` is a system specific new line marker that might not work on all systems. – demongolem Apr 10 '14 at 20:18
  • 1
    @Airglow. When you add "\n" to Stringbuffer , it is just converted in to corresponding byte as other character does. But when display the characters in some editor/console , the bytes from String would convert into shapes based on ASCII/ Charset choosed. In Most of the charset ( in my mind 100%) would result next line for the byte equivalent to the '\n' . These are special instructions to the machine to do tab space line feed CR and so. – Mani Apr 10 '14 at 20:22
  • @demongolem Is there a system that "\n" wouldn't work on? I thought it was a standard that the JVM would always interpret as newline. – AntonH Apr 10 '14 at 20:22
  • @AntonH At least with StringBuilder this is the case, I am not 100% sure of StringBuffer. Like maybe you need `\r\n` or something of that sort. – demongolem Apr 10 '14 at 20:23
  • Newline will not work in log4j logging if you just add newline to the message and try to put it into the log. http://stackoverflow.com/questions/11843941/log4j-print-empty-line-to-logfile – bgth Apr 10 '14 at 20:27

5 Answers5

2

Adds new line marker to text that is actually in StringBuffer. Next part will be shown in the line below.

markubik
  • 646
  • 4
  • 9
2

It appends a "newline" character, thus ending the current line and beginning a new line.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44
1

You can read what \n and other characters mean in the Java tutorials:

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
...
\n Insert a newline in the text at this point.

Keppil
  • 45,603
  • 8
  • 97
  • 119
1

\n is a new line character. Here is an example:

System.out.print("hello\nmy\nname\nis\ntyler\n");

Outputs:

hello
my
name
is
tyler
Tyler
  • 17,669
  • 10
  • 51
  • 89
1

"\n" means line breaking, in the following example the output will be

a
b

ex code:

StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("a");
stringBuffer.append("\n");
stringBuffer.append("b");
System.out.println(stringBuffer.toString());
DanW
  • 247
  • 1
  • 9