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.
What is the prominent difference between both if any?.
%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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With