Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Arrays

Why are we using %n for newlines instead of \n in the JacksonsOldSchool.java for loop with arrays example?

Newline was accomplished by using \n previously. But in the JacksonsOldSchool.java for loop with arrays example, I encountered %n being used for newline. Please explain.

2 Answers

You should use the %n version as it is platform-independent. Craig mentioned at some point in some later video that he was not aware of it and he got to know it via the forum when someone commented on it, if I remember correctly.

Thanks for the answer @Pedro Cabral. Did a bit of search on google and found the below mentioned explanation: %n is portable between various platforms, the value emitted from %n will suits underlaying platform, where as 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.

Reference URL: http://stackoverflow.com/questions/31981136/difference-between-n-and-n-for-printing-a-new-line-in-java