How System.out in Java works

The first line of code that new developers encounter when they write their first Java program typically includes a call to System.out.println(), as in the following line of code:

System.out.println("Hello World!");

But what exactly does this line of code mean, and how can we explain it in plain English?

Meaning of System.out.println() in Java

As is often the case in Java, the best way to explain System.out.println(“Hello World”) is to read it from right to left.

System.out.println(“Hello World”) means: take the provided text (“Hello World”) and display it (print) in the output window (out) associated with the program (System) running this Java code.

The four key components associated with this line of code are as follows:

  1. The System class that provides standard mechanisms to obtain input, write output and log error messages.
  2. The PrintStream named out which can be configured to write text to either the console window or to log files.
  3. The print method which is passed a text String to print out.
  4. The text String provided as an argument to the print method.

System’s overloaded println method

The System.out.println() method is incredibly versatile because it is overloaded to support a variety of objects and data types. As a result, it has the ability to print details about the following values:

  • String
  • boolean
  • int
  • long
  • byte
  • short
  • double
  • float
  • char

Furthermore, the println method invokes the toString() method of any objects passed as an argument, so it has the ability to display details of instances of Java classes.

It can even be left blank just to force the generation of a newline.

What System.out.println means

The System class is one of the foundation classes in Java.

System out println examples

Here are some examples of how to use the System.out.println() method call to display the values of various Java data types and values:

int y = 20; 
System.out.println();    // prints a new line
System.out.println(y);   // prints 10
System.out.println(10);  // prints 10 
System.out.println('x'); // prints x
System.out.println(10.10);   // prints 10.10
System.out.println(10!=10);  // prints false
System.out.println(Math.PI); // prints 3.14...
System.out.println("Hello World!); // prints Hello World

Tips and tricks with System.out.println()

New developers often get tripped up on the syntax of the System.out.println() method call. Here are a few things to remember:

  • Java is case sensitive, so System must lead with an upper-case letter S.
  • Every line of code in Java must end with a semi-colon. If you forget it, your code won’t compile.
  • Any multi-character text passed as an argument to the println method must be in “double quotes”, not ‘single quotes’.
  • You can only use single-quotes to pass a single character to the println method.

Have fun working with the System object and printing data to the console or terminal window as you learn how to program in Java. It makes it easier to get feedback and make sure your program is working properly.

 

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close