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 trialShay Paustovsky
4,281 PointsFormat Specifiers - Java
I didn't understand the format specifiers and parameters in/for the printf(); functions I understood it replaces the string but nothing more than that. If someone could explain this to me please I would be thankful. :)
2 Answers
Kent ร svang
18,823 PointsIt isn't really much more than that to understand. It is just a way of getting variables and other input within a string. e.g., if you have this variables :
String name = "Bob";
int age = 24;
..and you want to output this variables to the console, theres a couple of ways to do it. One way is concatenate it directly within the string, like this:
System.out.printf(bob + " is " + age + " years old.");
another way is to use the format specifiers:
System.out.printf("%s is %d years old", name, age);
My preference is the latter, since it is cleaner and more compact. A lot of concatenation can look pretty messy.
Here are som reading material: https://alvinalexander.com/programming/printf-format-cheat-sheet
Hope this helped you in some way.
Shay Paustovsky
4,281 PointsThank you Kent, I understood the format specifiers as I was learning further. I guess it's something that you understand when you actually encounter it.
Have a nice day!