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

Naomi Mitchell
PLUS
Naomi Mitchell
Courses Plus Student 782 Points

What is the difference between console.printf() and System.out.println()?

Hi i am currently learning java and have come across two ways of printing out text to the screen. One way is System.out.println() the other way i have found is console.printf().

Which one is right? Why is it right? When should i apply this line of code and run it? Are there other lines of code similar to these two? Does the console or anything else affect which one i use? Can i use both lines of code in Android SDK and IntelliJ or only one specific line for each?

If i started learning java with a different online platform for example (not leaving LOVE it here!) would i learn a completely different version of java and different way of doing things or would it be the same?

1 Answer

This post explains difference between Console and System.out: Difference between using Console vs. System.out

And this is more specific to System.out.print and console.printf: Difference between System.out.print and console.printf

In the post above the print are mentioned instead of println, but the only difference between the two is that print prints out on the same line, and println prints out on a new line. I can show you an example of using print, println, or printf:

// print:
System.out.print("one");
System.out.print("two");
// Prints: 
// onetwo

// println:
System.out.println("one");
System.out.println("two");
// Prints: 
// one
// two

// printf:
System.out.printf("one %s", "two"); // %s specifies a string and start looking at the second parameter, which is "two"
// Prints:
// one two

// Your could add more and other data types if you like:
System.out.printf("one %s %d", "two", 3);
// Prints:
// one two 3

// Or use variables:
System.out.printf("one %d %s", int_variable, string_variable);
// Prints:
// one int_variable_value string_variable_value