Alexander Claes
← Learning Java

Hello World

My first small Java program, broken down piece by piece to understand what every keyword and symbol is doing.

Alexander Claes Alexander Claes 3 min read
Learning Java
A minimal console displaying Hello, world connected to a translucent globe

After learning about the different programming styles Java supports, I wanted to take a step back and look closely at the smallest working program I could write. Hello, world is simple on purpose: there is almost no behaviour to distract from the structure Java expects. Coming from front-end development, some parts feel familiar, while others are more explicit than they would be in JavaScript.

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

What every part is doing

There is quite a bit wrapped around a single line of output. Breaking it down once helps me see which parts are Java syntax and which are names I could choose myself.

public class HelloWorld {

  • public is an access modifier. It makes the class accessible from anywhere.
  • class tells Java that I am declaring a class. The class acts as the container for this small program.
  • HelloWorld is the name I gave the class. Java class names conventionally use PascalCase. Because the class is public, the source file must also be named HelloWorld.java.
  • The opening curly brace { begins the body of the class. Everything until its matching closing brace belongs to HelloWorld.

public static void main(String[] args) {

  • public makes the method accessible to the Java Virtual Machine, so it can start the program.
  • static means the method belongs to the class itself. Java can call it without first creating a HelloWorld object.
  • void is the return type. It says that this method does not return a value when it finishes.
  • main is the method name Java recognises as the program’s entry point. Execution begins here.
  • The opening parenthesis ( starts the method’s parameter list, and the closing parenthesis ) ends it.
  • String is Java’s type for text. It starts with a capital letter because String is a class.
  • The square brackets [] turn String into an array type, so the method can receive zero or more pieces of text.
  • args is the name of that array. It contains any command-line arguments supplied when the program starts. The name can be changed, but args is the usual convention.
  • The opening curly brace { begins the body of the main method.

System.out.println("Hello, world");

  • System is a class from Java’s standard library. It is available automatically through the java.lang package.
  • The first dot . accesses a member of the System class.
  • out is a static field on System. It represents the standard output stream, which is normally the terminal or console.
  • The second dot . accesses a method on the object stored in out.
  • println prints a value and then moves the cursor to a new line. Its name is short for “print line.”
  • The opening parenthesis ( begins the arguments passed to println, and the closing parenthesis ) ends them.
  • The double quotation marks " delimit a string literal: text written directly in the source code.
  • Hello, world is the value of that string. The comma and space are ordinary characters inside the text and are printed exactly as written.
  • The semicolon ; marks the end of the statement.

The closing braces

  • The first closing curly brace } ends the body of the main method.
  • The final closing curly brace } ends the body of the HelloWorld class.

The spaces and indentation do not change what the program does, but they make its nested structure much easier to see. The program itself is not doing much yet, of course, but it gives me a useful first mental model: code lives inside a class, Java starts in main, and a statement ends with a semicolon. That is enough structure to start building on.