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 trialEric Chan
4,254 PointsDifference between system.out.printf and system.out.println
I know this video is not about System.out, but I have been wondering what the difference between .printf and .println is.
Can someone help me clear the cloud? Thanks
2 Answers
Brendon Butler
4,254 Pointsprintln is used when you want to create a new line with just simple text or even text containing concatenation. ("Hello " + "World" = "Hello World.")
printf is used when you want to format your string. This will clean up any concatenation. For a simple example -> ("Hello, " + username + "! How are you?") could easily be cleaned up a bit by using ("Hello, %s! How are you?", username). It's up to you really when you want to use this.
I would definitely recommend it if you are using multiple variables. In that case you would use multiple conversions in your string. Please feel free to read up on the Formatter Documentation as it will help you understand it better and it provides a full list of available conversion characters.
Gavin Ralston
28,770 PointsNo reason to add another answer here, as you covered the differences that the question is probably looking for.
I would say that another reason to use printf() is when you want to format the output in a console app. You can add tabs, zero-fill columns to make numbers line up neatly, etc.
The println() way of doing things would be to hard code spaces and tabs and run things a hundred times til everything lined up, or writing your own functions to format strings before plugging the string into the println method.
chrisverra
3,760 Pointsas told by some guy on stack overflow:
println() prints a new blank line and then your message. printf() provides string formatting similar to the printf function in C. printf() is primarily needed when you need to print big strings to avoid string concatenaion in println() which can be confusing at times. (Although both can be used in almost all cases).
Eg. int a = 10;int b = 20;
println("a: " + a + " b: " + b); //Tedious String Concatenation
printf("a: %d b: %d\n", a, b); // Output using string formatting.
While printf() enables you to print fractional outputs up to any decimal place in a single line, the same task using println() can get very complex.
Eg. printf("%.6f",x); // prints x up to 6 decimal places.
hope this helps
Eric Chan
4,254 PointsEric Chan
4,254 Pointsnow i get it! thanks guys for your help!