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 don't we use System.out.println("")?

I have learned Java in college and my professor did tell me about the method we used in the video. However, he said that it is easier to just use System.our.println(""); and print out what we desire. Is there any specific reason why we are creating an object for console and then calling the printf method on it?

Thanks

5 Answers

What Grigorij says is not entirely true. you can use it with variables "no change needed" aswel

you can use both, with println you can concatinate

example:

String myName = "Chris";
int age = 27;
System.out.println("My name is " + myName " and I am " + age + " years old.");

you would get: My name is Chris and I am 27 years old.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Great point Christ,

thanks for completion

:thumbsup:

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Siddhesh,

printf method gives you the possibility to use formatters like %s or %d. So the code is more flexible and looks cleaner.

For example you create two variables:

String name = "Siddhesh";
String last lastName = "Gannu";

The println looks like this and can create it without the variables.

System.out.println("My name is Siddhesh Gannu");

But if you change the name variables to something else you will need to change the names inside the parenthesis.

// change needed
System.out.println("My name is newName newLastName");

If you use the printf method you can use the old code:

System.out.print("My name is %s %s", name, lastName );
// no change needed

Does it make sense?

Grigorij

But can't we do that with both? Or does it only work in System.out.println

right, good point grigorij, i forgot to mention the new line :)

I don't see a reason to pick a way other than personal preferences. Both can concatinate, both can use variables.

I have no idea what would be better.

I tend to use system.out.println, because then i can see what gets written more easely

with system.out.print i have to look and put the variables in place to see what gets written

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Here some more details:

  • print(), printf() and prinln() are methods of the PrintStream class
  • Both print() and printf() will print the text in the same line without entering a new one
  • println() will give you a new line after the text is over and allows you formatting the text

Yeah Buddy!