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

What is different between console.printf and System.out.println ?

When I learn Java from school, we use System.out.println, may I know what is the different from console.printf ?

1 Answer

Hi Yongshuo Wang,

println() will print a string with a newline at the end - and will not accept a formatted string. printf() will print a formatted string - but will not add a newline to the end.

Both functions can be called from System.out.

String message = "Hello World!";

// the output from these two lines are equivalent
System.out.println(message);
System.out.printf("%s\n", message);

Thanks for your answer.