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 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.