Hello World
My first small Java program, broken down piece by piece to understand what every keyword and symbol is doing.
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 {
publicis an access modifier. It makes the class accessible from anywhere.classtells Java that I am declaring a class. The class acts as the container for this small program.HelloWorldis the name I gave the class. Java class names conventionally use PascalCase. Because the class is public, the source file must also be namedHelloWorld.java.- The opening curly brace
{begins the body of the class. Everything until its matching closing brace belongs toHelloWorld.
public static void main(String[] args) {
publicmakes the method accessible to the Java Virtual Machine, so it can start the program.staticmeans the method belongs to the class itself. Java can call it without first creating aHelloWorldobject.voidis the return type. It says that this method does not return a value when it finishes.mainis 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. Stringis Java’s type for text. It starts with a capital letter becauseStringis a class.- The square brackets
[]turnStringinto an array type, so the method can receive zero or more pieces of text. argsis the name of that array. It contains any command-line arguments supplied when the program starts. The name can be changed, butargsis the usual convention.- The opening curly brace
{begins the body of themainmethod.
System.out.println("Hello, world");
Systemis a class from Java’s standard library. It is available automatically through thejava.langpackage.- The first dot
.accesses a member of theSystemclass. outis a static field onSystem. It represents the standard output stream, which is normally the terminal or console.- The second dot
.accesses a method on the object stored inout. printlnprints 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 toprintln, and the closing parenthesis)ends them. - The double quotation marks
"delimit a string literal: text written directly in the source code. Hello, worldis 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 themainmethod. - The final closing curly brace
}ends the body of theHelloWorldclass.
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.