Your first Java program

Introduction

Java is one of the most widely used programming languages in the world. It runs the back ends of banks, airlines and online shops, every Android app has Java roots, and universities teach it as the language of object-oriented programming. Java's motto is "write once, run anywhere": the same compiled program runs on Windows, Linux and macOS. This course teaches Java 17 from the first program to classes, collections and streams, and every example runs in your browser.

How it works

Java code is written in classes. A program needs one class with a main method, the place where it starts:

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

For now, read the long first line of main as a fixed formula; each word will make sense in later lessons. Statements end with a semicolon, blocks are wrapped in braces, and // starts a comment.

Running a Java program takes two steps. First javac, the compiler, reads the whole file, checks it, and turns it into bytecode. If anything is wrong, such as a missing semicolon or a misspelt name, it stops with an error naming the line, and nothing runs. Then the JVM, the Java Virtual Machine, runs the bytecode. Here both steps happen when you press Run.

Three methods print. System.out.println(x) prints and ends the line; System.out.print(x) prints without ending it; System.out.printf(format, values...) fills a format: %d for a whole number, %s for text, %.2f for a number with two decimals, and %n for a new line.

Input is read with a Scanner, imported from java.util at the top of the file:

Scanner in = new Scanner(System.in);
String word = in.next();      // the next word
String line = in.nextLine();  // the rest of the line
int n = in.nextInt();         // the next whole number
double x = in.nextDouble();   // the next number with decimals

One trap: after nextInt(), the rest of that line, often just its newline, is still waiting. A nextLine() straight after it returns that empty rest. Read the line first, or call nextLine() once to skip it.

Example

// Every Java program lives in a class. Running it starts at main.
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
        Scanner in = new Scanner(System.in);
        String name = in.nextLine();
        int age = in.nextInt();
        System.out.println("Welcome, " + name + ".");
        System.out.print("You were born around ");
        System.out.println(2026 - age);
        System.out.printf("%s has %d letters%n", name, name.length());
    }
}

Input used: Ada ⏎ 36

Open in compiler

Line by line

  1. import java.util.Scanner; makes Scanner available in the file.
  2. main prints a greeting, then creates a Scanner on the standard input.
  3. nextLine() reads the whole first line, and nextInt() the number on the next one.
  4. print leaves the line open, so the following println continues it.
  5. printf fills %s with the name and %d with its length, and %n ends the line.

Expected result

With this input:

Ada
36

it prints:

Hello, Java!
Welcome, Ada.
You were born around 1990
Ada has 3 letters

In the real world

Java runs on billions of devices. Large companies use it for servers because the JVM is fast, stable and well understood, and because Java's strict compiler finds many mistakes before the code ships. Build tools such as Maven and Gradle compile whole projects with javac, and IDEs such as IntelliJ IDEA show compile errors as you type.

Common mistakes

  • Forgetting a semicolon or a closing brace: javac reports the line where it noticed.
  • Writing the class name differently from the file name: a public class Main must be in Main.java.
  • Calling nextLine() right after nextInt() and getting an empty string.
  • Mixing up print and println, so two outputs run together on one line.

Best practices

  • Read the first compile error first; later errors are often caused by it.
  • Create one Scanner and use it for all input.
  • Use printf when numbers need a fixed number of decimals.

Summary

A Java program is a class with a main method. javac compiles the whole file to bytecode, stopping at any error, and the JVM runs it. println, print and printf write output, and a Scanner reads words, lines and numbers from the input.

Quick check

1. Where does a Java program start running?
2. What does javac do?
3. Which call prints text without ending the line?
4. What does nextLine() return right after nextInt() read the last number on a line?

Key takeaways

  • A Java program is a class whose main method runs first; every statement ends with a semicolon.
  • javac checks and compiles the whole program before anything runs; the JVM then runs it.
  • println prints a line, print prints without a newline, printf formats; Scanner reads input.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.