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

What is the difference between using System.out.println vs System.out.printf?

I am just little confused of when I will use one over the other. I just feel that they both do the same thing.

2 Answers

printf helps the programmer to output the result in a formatted way. for example if you are to print an invoice that has an item and price. If you use the println the output will not be adjusted in nice way it might look like

item price

water $1.50

pen $2.3

ice-cream $3.0

However, when you use printf, you can align all the prices and the output will look nicer.

Hope this helps.

Thanks, really helps!

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

System.out.println does not do string formatting (which we have been using to replace the %s) but does add a new line automatically.

Ah ok, makes sense, thanks!