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 Java Objects Meet Objects Creating Classes

First Question: What is the difference between %n and \n, don't they both make new lines?

Is there any difference between them?

1 Answer

Matthew Caloger
Matthew Caloger
12,903 Points

According to the (Offical Oracle Java Documentation)[https://docs.oracle.com/javase/tutorial/java/data/numberformat.html]

The %n is a platform-independent newline character. [...] You should always use %n, rather than \n.

Note that this only works while using a "format" output,. if you were to use a plain old System.out.print(ln), it would act as a regular string.

System.out.println("using \n slash");
        System.out.println("using %n percent");

->

using 
 slash
using %n percent

\n is, specifically, a UNIX (Linux/macOS) newline, but is often converted by Windows into "\r\n\ for "return-feed-newline", and is effective when working with strings regularly.