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 trial

Java Java Objects Meet Objects Creating Classes

G H Mahimaanvita
G H Mahimaanvita
16,234 Points

println & printf

Can printf perform all the actions that println does?

1 Answer

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

Example.

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.

Example.

printf("%.6f", x);  // prints x up to 6 decimal places.