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 Objects Meet Objects Methods

John Anselmo
PLUS
John Anselmo
Courses Plus Student 2,281 Points

println vs printf?

I accidentally put in System.out.println("The dispenser is %s %n", dispenser.getCharacterName()); and it didn't work. I noticed my mistake and changed it to printf. So I'm just wondering what the difference is, why %s and %n work in one, but not the other, and if there are any other things like that for println vs printf.

1 Answer

Hi John,

The f bit is the formatter; that's what adds the %s functionality, among others. There are numerous placeholders available, rather than just strings. Have a read here for various number options.

Steve.

I don't know, in short.

There may be an overhead issue with using printf against print or println. It may be bad practice to use something with built-in functionality and then not use that functionality. It may be that some restricted systems, such as embedded systems, may not come fully-loaded and always have standardised pre-defined output, thus negating the need for printf.

But I don't know so will pass this over the the man himself ... Craig Dennis

Sorry I can't be of more use with that.

Steve.