Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Getting Started with Java Receiving Input

Steven Couture
Steven Couture
2,106 Points

Why use console.printf instead of System.out.println? or will that be discussed later?

?????

4 Answers

Zac Mazza
Zac Mazza
5,867 Points

System.out.println is able to print using variable substitution, but by using printf you can simplify mixing variables with text. For example, instead of System.out.println("Hello, my name is " + yourName + " and I am " + age + " years old.), I can instead use printf and say console.printf("Hello, my name is %s and I am %i years old.", yourName, age);. When using several variables in your output, it can be beneficial to use printf. You can also use this to format the output of the variables, such as converting numbers to dollar amounts.

Hope this helps.

Zac

Simon Coates
Simon Coates
28,693 Points

is that what he was asking? I assumed it was ultimately about why you'd go with the Printstream object over the Console object.

Lucas van der Laan
Lucas van der Laan
4,026 Points

Printf makes it shorter AND easier to use variables in your output.

It also is useful for if you want to print x, where x = 0.50 instead of 0.5000001;

double x = 0.5000001;

Zac Mazza
Zac Mazza
5,867 Points

Good point, and I'm not sure the answer to that. Ultimately it comes down to making the I/O of System easier to access. One of the videos mentions that Console is a wrapper class.

printf is available for both System.out and Console. I suppose it comes down to preference. :-)