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 (Retired) Harnessing the Power of Objects Exceptions

println and printf have different behaviour?

Seems that System.out.println - does not permit %s things - gives error at compiling and printf - works ok.

2 Answers

That's right! Printf is short for Print Format, and Println is short for Print Line. %s stuff is called formatting, so that means that printf supports formatting and println doesn't. :)

Hope this helps, Alex

Piyush Patel
Piyush Patel
17,253 Points

Yeah, printf() takes another arguments to print whatever you want to print. It actually formats the message and then prints. For example

System.out.printf("The error was: %s\n", iae.getMessage());

Here, we are telling the compiler to add the message from iae.getMessage() and replace '%s'.

Println only prints the line. Whatever you supply as argument will be printed.