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 Basics Getting Started with Java Introduction to Your Tools

Why do you use the console.printf() instead of System.out.println();?

Why do you use the console.printf() instead of System.out.println();?

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Erik,

printf prints a string with optional format specfifiers so you can format how the string will. If for example you wanted to add a new line at the end of your string you would do

Why do you use the console.printf("Enter your name:  %n");

So %n would add a new line.

But with System.out.println() the new line comes by default and you don't add a format specifier.

You can Google your question for more information on the difference between the 2 methods. Here's a good link I found. https://www.quora.com/Whats-the-difference-between-println-and-printf-in-Java :-)

Why does the course use console instead of system.out then? Is console more standard and widely use?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

As far as I'm aware it's personal preference, or to put it another way what is best for use in your project. If you just want to display a message you might prefer to use use println();

They're each standard Java methods and as far as I;m aware neither are deprecated