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 trialRafał Pawłow
1,580 PointsWhat 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
Michael Jurgensen
8,341 PointsFrom 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.
Anas Mourad
14,817 PointsIn 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
2,003 PointsCan 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?
Ashish Shah
1,468 PointsWhat does working in console means?
Arshaan Bhimani
1,246 PointsIf it clears it up at all. You're basically calling: System.out.print() and System.console.printf()
Ho Sun Lee
2,003 PointsHo Sun Lee
2,003 PointsIt does, thanks so much for clearing that up!
Rafał Pawłow
1,580 PointsRafał Pawłow
1,580 PointsGreat answer, thank you so much!
Michael Coons
202 PointsMichael Coons
202 PointsThank you for asking that question. I already took a Java class before and the printf confused me.