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) Meet Objects Creating Classes

Difference between console.printf and System.out.println?

From the video, I can see that console.printf and System.out.println is mostly the name (minus the \n built into the system out). What is the difference between these two functions?

2 Answers

rydavim
rydavim
18,813 Points

Welcome to Treehouse!

Disclaimer: I do not have much experience with Java, but I'm familiar with the basics and have experience with other languages.

I believe that println is fine if you just need to print a line of text, you can read this as "print line".

You would use printf if you need to format the text in some way, such as justification or including dynamic values. You can read this as "print with formatting".

// Example of a formatted string including variables.
String noun = "fox";
String adjective = "lazy";
System.out.printf("The quick brown %s jumps over the %s dog.", noun, adjective);

Sorry I can't give you a better example, but hopefully this helps clarify why you might use printf sometimes during testing.

Thaks A lot..That Helps Me a lot :)