Java Java Objects Meet Objects Methods

John Anselmo
PRO
John Anselmo
Pro 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

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,656 Points

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.

John Anselmo
John Anselmo
Pro Student 2,281 Points

Why not just use printf all the time then?

Steve Hunter
Steve Hunter
Treehouse Moderator 57,656 Points

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.