ESC
Type to search...
Dashboard / Java notes / Concepts / Basics / Part i

00 - Printing

P1 · Updated · Source
#java/basics #l0 #concept #must-know #status/known

Printing to Screen

What is it?

Printing means sending text output to the console (terminal). In Java, you do this with System.out.println().

Why does it exist?

Every program needs a way to communicate results to the user. Printing is the most basic form of output - you’ll use it constantly for debugging and displaying results.

How does it work?

System.out.println("Hello World");
  • System → a built-in Java class that gives access to system-level tools
  • out → the output stream (i.e., the console)
  • println → the method that prints text and then moves to a new line
  • "Hello World" → the parameter (the value you’re passing into the method)

println vs print

Method Behaviour
System.out.println("hi") Prints “hi” and moves to a new line
System.out.print("hi") Prints “hi” and stays on the same line
System.out.print("Hello ");
System.out.print("World");
// Output: Hello World   (on ONE line)

System.out.println("Hello");
System.out.println("World");
// Output:
// Hello
// World

The Boilerplate (Minimum Java Program)

Every single Java program needs this shell to run. Without it, Java won’t even start executing:

public class Example {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Breaking it down line by line:

Part Meaning
public class Example Declares a class named Example. The filename must match: Example.java
public static void main(String[] args) The entry point - Java starts executing from here
{ ... } Curly braces define the boundaries of blocks
System.out.println(...) The actual code you want to run

Important The class name in the code must exactly match the filename. ExampleExample.java. Case-sensitive.

Note public, static, void, class are keywords - reserved words with special meaning in Java. You’ll understand each one as you progress. For now, just use the boilerplate as-is.

How Commands Execute

Commands run one line at a time, top to bottom. Java reads your code like you read a book - left to right, line by line.

System.out.println("First");
System.out.println("Second");
System.out.println("Third");
// Output:
// First
// Second
// Third

Gotchas

  • Forgetting the semicolon ; at the end of each statement → compilation error
  • Mismatching class name and filename → Java won’t compile
  • System with a capital S - it’s case-sensitive; system won’t work
  • String content must be in double quotes "...", not single quotes '...'

Related: Input, Methods, String