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

What is the difference between System.out.print and console.printf ?

What is the difference between System.out.print and console.printf ? I've never seen that before, and my tutor in college told us to use print or println. Thanks in advance!

4 Answers

From my understanding we are using console.printf since we are working in the console. The reason Console exists is to provide features that are useful in the specific case that you're being run from an interactive command line:

secure password entry (hard to do cross-platform)
synchronisation (multiple threads can prompt for input and Console will queue them up nicely, whereas if you used System.in/out then all of the prompts would appear simultaneously).

System.out.print is a static call which means its faster. You also don't need to create an instance when working with System.out.print.

Keep in mind that the class console will only work in the console. If you are not using the console you should use System.out.print.

Hope that helps.

Ho Sun Lee
Ho Sun Lee
2,003 Points

It does, thanks so much for clearing that up!

Great answer, thank you so much!

Thank you for asking that question. I already took a Java class before and the printf confused me.

Anas Mourad
Anas Mourad
14,817 Points

In programming, you can achieve the same thing in different ways. Console class has its own functions http://docs.oracle.com/javase/6/docs/api/java/io/Console.html . And System class has its own functions too http://docs.oracle.com/javase/7/docs/api/java/lang/System.html . Read methods of both so you can decide which one to use. I hope this helped a bit.

Ho Sun Lee
Ho Sun Lee
2,003 Points

Can you go a little more into detail? When is the correct time to use either? I've been using System.out.print("") in my class text book, but here, we're using console.printf.

Is there a specific time to use console.printf as opposed to System.out.print?

What does working in console means?

Arshaan Bhimani
Arshaan Bhimani
1,246 Points

If it clears it up at all. You're basically calling: System.out.print() and System.console.printf()