Let’s deep dive System.out.println in Java

Swaroop Nadella
2 min readAug 22, 2024

--

First Program in Java — System.out.println( )

System.out.println is a commonly used method in Java for printing messages to the console.

It comes from the ‘java.lang’ package, which is automatically imported into every Java program.

Here’s a breakdown of what each part means:

1. System: This is a final class in Java that provides access to system-related resources. It includes fields and methods for standard input, output, and error streams, among other things.

2. out: This is a static member of the ‘System’ class.
It represents the standard output stream, which is typically the console. ‘out’ is an instance of ‘PrintStream’, a class that provides methods to print data to various output streams.

3. println: This is a method of the ‘PrintStream’ class.
It prints the argument passed to it (such as a string, integer, etc.) to the console, followed by a newline character.
The ‘println’ method is overloaded, meaning it can accept different types of arguments (e.g., strings, numbers, characters).

How It Works:
When you call — System.out.println(“Hello, World!”);

here’s what happens:

- System.out : accesses the standard output stream.
- println(“Hello, World!”) : prints the string “Hello, World!” to the console and then moves the cursor to the next line.

Example:
public class Main {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
System.out.println(123);
System.out.println(true);
}
}

In this example, the ‘println’ method is used three times to print a string, an integer, and a boolean value to the console.

Storytelling Example:
Imagine you’re writing a letter (the message you want to print) and ‘System.out’ is like a mail carrier that delivers your letter to the mailbox (the console).

The ‘println’ is the act of the mail carrier placing the letter in the mailbox and closing the lid (moving to a new line), ready for the next letter.

This method is particularly useful for debugging purposes, as it allows you to quickly see the output of your program during execution.

Swaroop Nadella
Test Automation Engineer, Tech Educator

Subscribe here, to Get an email whenever I publish article on Medium

Core Java and Coding for Automation Testers — Udemy Course

Swaroop Nadella Academy Store — Quizzes and Courses

--

--