144

I'm reading Effective Java and it uses %n for the newline character everywhere. I have used \n rather successfully for newline in Java programs.

Which is the 'correct' one? What's wrong with \n ? Why did Java change this C convention?

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
ripper234
  • 222,824
  • 274
  • 634
  • 905

8 Answers8

197

From a quick google:

There is also one specifier that doesn't correspond to an argument. It is "%n" which outputs a line break. A "\n" can also be used in some cases, but since "%n" always outputs the correct platform-specific line separator, it is portable across platforms whereas"\n" is not.

Please refer https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Original source

Dhwaneel
  • 541
  • 5
  • 8
Bill K
  • 62,186
  • 18
  • 105
  • 157
  • 1
    @user2864740 That's true, it doesn't output the _correct_ platform-specific line separator but the _current_ one. – Hauke Ingmar Schmidt Apr 13 '14 at 12:09
  • 1
    umm, what does that mean? – Kalpesh Soni Mar 27 '18 at 17:11
  • 2
    I believe it uses the one for the current OS that it's running on, but if it's running on Linux and outputting code for windows it may not be what you are after. I'm not otherwise sure what the other two commenters may have been referring to. – Bill K Mar 27 '18 at 17:19
  • 1
    the phrase "correct platform-specific line separator" comes from Oracle docs itself, and is shorthand for "correct separator for the current platform that the bytecode is running on", and thus the original comment from Apr 13 2014 was just confused. – xdavidliu Sep 10 '22 at 02:56
41

%n is portable across platforms \n is not.

See the formatting string syntax in the reference documentation:

'n' line separator The result is the platform-specific line separator

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • 1
    See the https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#dls – Namo Jan 16 '19 at 09:34
29

While \n is the correct newline character for Unix-based systems, other systems may use different characters to represent the end of a line. In particular, Windows system use \r\n, and early MacOS systems used \r.

By using %n in your format string, you tell Java to use the value returned by System.getProperty("line.separator"), which is the line separator for the current system.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Craig Putnam
  • 386
  • 2
  • 2
  • You can use `System.lineSeparator()` method instead of `System.getProperty("line.separator")` as an alternative ;) – NikolaS Oct 24 '22 at 14:19
24

Warning:

If you're doing NETWORKING code, you might prefer the certainty of \n, as opposed to %n which may send different characters across the network, depending upon what platform it's running on.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
George Forman
  • 600
  • 5
  • 7
9

"correct" depends on what exactly it is you are trying to do.

\n will always give you a "unix style" line ending. \r\n will always give you a "dos style" line ending. %n will give you the line ending for the platform you are running on

C handles this differently. You can choose to open a file in either "text" or "binary" mode. If you open the file in binary mode \n will give you a "unix style" line ending and "\r\n" will give you a "dos style" line ending. If you open the file in "text" mode on a dos/windows system then when you write \n the file handling code converts it to \r\n. So by opening a file in text mode and using \n you get the platform specific line ending.

I can see why the designers of java didn't want to replicate C's hacky ideas regarding "text" and "binary" file modes.

Peter Green
  • 91
  • 1
  • 1
6

Notice these answers are only true when using System.out.printf() or System.out.format() or the Formatter object. If you use %n in System.out.println(), it will simply produce a %n, not a newline.

user1677663
  • 1,431
  • 15
  • 21
4

In java, \n always generate \u000A linefeed character. To get correct line separator for particular platform use %n.

So use \n when you are sure that you need \u000A linefeed character, for example in networking.
In all other situations use %n

Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42
0

%n format specifier is a line separator that's portable across operating systems. However, it cannot be used as an argument to System.out.print or System.out.println functions.

It is always recommended to use this new version of line separator above \n.

Piyush Patel
  • 1,646
  • 1
  • 14
  • 26