12

The documentation of Java specifies to use %n rather than \n for a newline.

As per the Oracle JavaSE Number documentation

A new line character appropriate to the platform running the application. You should always use %n, rather than \n.

If any, what is the prominent difference between both?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Ashish Krishnan
  • 422
  • 2
  • 8
  • 16
  • it is just a java convection – Raghavendra Aug 13 '15 at 06:40
  • 5
    This question is already answered: http://stackoverflow.com/questions/1883345/whats-up-with-javas-n-in-printf – Maiko Kingma Aug 13 '15 at 06:40
  • check prev. discussion... http://stackoverflow.com/questions/1883345/whats-up-with-javas-n-in-printf – Harish Aug 13 '15 at 06:41
  • Thanks. for the link – Ashish Krishnan Aug 13 '15 at 06:45
  • 4
    Btw, that "You should always use %n" is *really* misleading, IMO. You should use %n if you want to use the platform-specific newline... but that's not always the case - often you're formatting data for a network protocol, or for a file format that requires a specific line break. Sloppy documentation here, IMO. – Jon Skeet Aug 13 '15 at 06:48

2 Answers2

19

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

Windows system use \r\n, and early MacOS systems used \r.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • 5
    I'd say it's exactly the other way round - the characters emitted when you use `%n` are platform-dependent (they vary by platform) whereas `\n` will *always* emit U+000A. Now it could be that you *want* your code to be platform-dependent, so it will emit the idiomatic line break for your platform - but I don't think it's fair to say it's platform-independent. – Jon Skeet Aug 13 '15 at 06:45
  • What have mean by that the usasge of `%n` is portable between multiple platfomrs, so it platform indipendent in the user's aspect. – Abimaran Kugathasan Aug 13 '15 at 06:52
  • 1
    That's not what I think of when someone tells me that something is "platform independent" - that suggests that it behaves the same *regardless* of platform. I think your answer would probably be better if you removed the first paragraph. – Jon Skeet Aug 13 '15 at 06:56
  • hmm..what if it's an API emitting the text content? – pinkpanther Dec 05 '18 at 20:20
  • 1
    Dang, learn something new. Been using `\n` inside double quotes for 20 years expecting it to be platform specific line endings. Weird part is that I've never ever had an issue or noticed. Must mean my tool chain when copying and/or reading files has always been adaptable enough for me not to notice! `:--)` But I am always moving code between Windows, Linux, flavors of Unix and, occasionally, Mac. I'm always having to tweak line endings on script files and various other files. But not Java file output ... that I can remember ... doh! – Charlie Reitzel Aug 02 '22 at 23:17
2

%n is special code (placeholder) for new-line symbol (that might be \r\n or \n) in formatted string and \n is an actual symbol.

You can think of %n as a symbol that will be replaced with the \r\n or \n in the resulting string.