How System out in Java works.

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

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

But what exactly does this line of code mean and how can it be explained in plain English?

Meaning of System.out.println() in Java

As is often the case in Java, System.out.println(“Hello World”) is best explained by reading 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:

  1. The System class that provides standard mechanisms for obtaining input, writing output and logging 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 gets 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:

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

Furthermore, the println method will invoke 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.

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

System out println examples

Here are some examples of the System.out.println() method call being used to display the values of a variety of 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
  • 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
  • Single-quotes can only be used 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 getting feedback and making sure your program is working properly easy to do.

 

 

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close