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 Basics Getting Started with Java Introduction to Your Tools

Why Console.printf() and not System.out.println()

So I know the output for both methods seem to be essentially the same but is there a reason that you’re using one that require an input statement instead of System.out.println()

If they aren’t the same what would be your reasoning for using one over the other?

2 Answers

Tsenko Aleksiev
Tsenko Aleksiev
3,819 Points

.println() - it print the entered String and goes to the next line after finished printing .printf() - it prints formatted text. The easiest to explain is ("%s is learning to code", myNameVar ). printF() uses those kind of place holders, which are replaced with your variables AND it doesn't goes to new line after finished printing.

I hope this helps:

// setting our Object/String as foodCravings = pizza

Object foodCravings = "pizza";

//to call upon that object, we can use %s(for string/object), helpful for when you want to store values & call upon that object frequently

console.printf("I want some %s\n",foodCravings);

// here, we are forced to type out "pizza" as opposed to just calling upon it w/ the printf method

System.out.println("I want some pizza");

// we are using that println method again, but instead of typing pizza, we added our object from earlier. take note of the plus sign directly following the closed parenthesis

System.out.println("I want some " + foodCravings);