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 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,694 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. :-)