Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Introduction

In this tutorial, we'll explore the differences between System.console() and System.out.

2. System.console()

Let's first create a program to retrieve the Console object:

void printConsoleObject() {
    Console console = System.console();

Running this program from an interactive terminal will output something like [email protected]

However, running it from other mediums will throw NullPointerException as console object would be null.

Or, if we run the program as below:

$ java ConsoleAndOut > test.txt

then the program will also throw a NullPointerException as we are redirecting the stream.

The Console class also provides methods to read passwords without echoing the character.

Let's see that in action:

void readPasswordFromConsole() {
    Console console = System.console();
    char[] password = console.readPassword("Enter password: ");

This will prompt for the password, and it won't echo the characters while we type it.

3. System.out

Let's now print the object of System.out:


This will return something like java.io.PrintStream.

The output will be the same from anywhere.

System.out is used to print data to the output stream and there are no methods to read data. The output stream can be redirected to any destination such as file and the output will remain the same.

We can run the program as:

$ java ConsoleAndOut > test.txt

This will print the output to the test.txt file.

4. Differences

Based on the examples, we can identify some differences:

  • System.console() returns a java.io.Console instance when it's run from an interactive terminal – on the other hand System.out will return java.io.PrintStream object irrespective of the invocation medium
  • The behavior of System.out and System.console() is similar if we haven't redirected any streams; otherwise, System.console() returns null
  • When multiple threads prompt for input, then the Console queues up those prompts nicely – whereas in case of System.out all of the prompts appear simultaneously

5. Conclusion

We learned in this article about the differences between System.console() and System.out. We explained that Console is useful when an application is supposed to run from an interactive console, but it has some quirks which should be noted and taken care of.

As always, the complete code for this article is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!