Difference between %n and \n for printing a new line in Java

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


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

Tags:

Java