Your First Program

Let's take a Java program apart line by line. You'll learn the compact form that Java 25 introduced for small programs, and the traditional form with a class and a public static void main method that you'll see in almost all existing Java code.

The compact form

JAVA
void main() {
    System.out.println("Hello, World!");
}
Output
Hello, World!
  • void main() declares a method called main — the place where the program starts. void means it doesn't return a value.
  • The curly braces { } contain the method's body.
  • System.out.println(…) prints a line of text to the console.
  • The statement ends with a semicolon ;.

A file like this is a compact source file (Java 25+). Behind the scenes Java wraps it in a class for you.

The traditional form

JAVA
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Output
Hello, World!

This version works in every Java version, and it's what most tutorials, books and real projects use:

  • public class Main — all Java code lives inside classes. A public class must be in a file with the same name: Main.java.
  • public static void main(String[] args) — the classic entry point. public lets the JVM call it, static means it belongs to the class rather than an object, and String[] args receives command-line arguments.

You'll learn what each keyword means in the object-oriented lessons. Until then, this course mostly uses the compact form so examples stay short — but both are real, complete Java programs.

print vs println

JAVA
void main() {
    System.out.print("Hello, ");
    System.out.print("World");
    System.out.println("!");
    System.out.println("Next line");
}
Output
Hello, World!
Next line

println moves to a new line after printing; print doesn't. Java 25 also adds a shorter IO.println(…) and IO.print(…) — handy for small programs. This course sticks to System.out, which works everywhere.

Printing numbers and expressions

JAVA
void main() {
    System.out.println(42);
    System.out.println(3 + 4);
    System.out.println("3 + 4 = " + (3 + 4));
    System.out.println("3 + 4 = " + 3 + 4);
}
Output
42
7
3 + 4 = 7
3 + 4 = 34

Look at the last line: once text is involved, + joins things together, so "3 + 4 = " + 3 becomes text and then 4 is appended. Parentheses force the addition first.

Escape sequences

JAVA
void main() {
    System.out.println("Line one\nLine two");
    System.out.println("Name:\tAna");
    System.out.println("She said \"hi\"");
    System.out.println("C:\\Users\\ana");
}
Output
Line one
Line two
Name:	Ana
She said "hi"
C:\Users\ana
EscapeMeaning
\nNew line
\tTab
\"Double quote
\\Backslash

Compiler errors

JAVA
void main() {
    System.out.println("Missing semicolon")
}
Compiler error
Main.java:2: error: ';' expected

The compiler reports the file, the line number and what it expected. Reading error messages carefully — starting with the first one — is a core programming skill. Java is also case-sensitive: system.out.println or Main vs main are different names.

Try it yourself

Print a small receipt using print, println and tabs.

Try it yourself · Main.java
Edit the code, copy it into Main.java and run java Main.java (Java 25+). How to set up Java
Output
Item	Price
Coffee	2.50
Cake	3.20
Total	5.7