Java Java Basics Getting Started with Java Receiving Input

Steven Couture
Steven Couture
2,103 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,663 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. :-)