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 Strings and Variables

Philip Schultz
Philip Schultz
11,437 Points

Why do we use "console".

Why are we using console.printf("");. and not system.out.printf("");. What is the difference between the two and what will I be using the most?

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Phillip, the console does have some benefits, you can read input from users that is pretty difficult to do in the System class without some rigging, (most people just import another class like Scanner for this) Console is good because for interactive console environments, like the repl, it combines all the features of a scanner or BufferedReader. Essentially a console is more beginner friendly, but you'll notice that you outgrow it the more you work on your own projects, in fact they're a pain to set up in IDE environments.

Console will slowly become that cool awkward friend that you used to know when you dive into your own thing with java, we miss him.

There's no real big different between console.printf() and System.out.printf()

Thanks, hope this helps. Shout if it doesn't.

Philip Schultz
Philip Schultz
11,437 Points

Thank you for the response Rob. Could you please explain how and why they work. when I say console or system am i calling a method from a library or something. I tried to use the console demand on NetBeans but it would not work. Thanks Rob.

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Phillip both Console and System are classes in java. Which means that they're already built in the background, it's the same thing with Strings, they're pre built into the JDK source code that we download.

With that being said when you call one of these like System.out.println() that method is already built into java to know to print whatever is passed in as the argument to the internal console inside the IDE or whichever development environment you might be using.

As far as being able to use console in IDE's they take a lot of time to set up, as the IDE's usually have their own internal console, it's easier to use a class like Scanner to import one if you need to get user information.

I've included some impromptu code below to show how to import a scanner into your environment, and how to use like a console object.

import java.util.Scanner;

Scanner sc = new Scanner(System.in);

System.out.println("Hello, what is your Name?");
String name = sc.nextLine();
System.out.printf("I see, so your name is %s \n", name);

This would catch whatever input the user put for "name" and pass that value into name, you'll notice that we have to use both System.out.println to set up the variables, in IDE environments you unfortunately have to use both, as consoles really don't work in an IDE environment.

For the next step, let's way you wanted to get an int

System.out.println("how many do you want?");
int amount = sc.nextInt();
System.out.printf("Well then here is %d \n", amount);

Thanks, sorry if I threw too much information than what was asked for.