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 trialJohn Anselmo
Courses Plus Student 2,281 Pointsprintln 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
57,712 PointsHi 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
Courses Plus Student 2,281 PointsJohn Anselmo
Courses Plus Student 2,281 PointsWhy not just use printf all the time then?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI don't know, in short.
There may be an overhead issue with using
printf
againstprint
orprintln
. 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 forprintf
.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.