Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,818 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);