BeginnerJava · Lesson 1 of 8

Setting up Java

Install the JDK, understand the main method, and run your first program.

Java is a statically typed, object-oriented language that runs on the Java Virtual Machine (JVM). It powers banking systems, Android apps, large web back-ends and big-data tools. Install a JDK (Java Development Kit) — version 21 LTS or newer — from adoptium.net, then check with java -version.

Every Java program starts in a main method inside a class. The file name must match the public class name: class Hello lives in Hello.java.

Since Java 11 you can run a single source file directly with java Hello.java — ideal for learning. Larger projects compile with javac (or a build tool such as Maven) into .class files that the JVM runs. IntelliJ IDEA Community and VS Code with the Java extensions are excellent free editors.

Hello.javaJava
import java.time.Year;

public class Hello {
    public static void main(String[] args) {
        String name = "Dolese";
        int year = Year.now().getValue();
        System.out.println("Hello, " + name + "! Welcome to Java in " + year + ".");
    }
}
TerminalShell
java -version
java Hello.java            # compile and run in one step

javac Hello.java           # or: compile to Hello.class ...
java Hello                 # ... then run the compiled class

Key points

  • Programs start at public static void main(String[] args).
  • A public class must live in a file with the same name.
  • java File.java runs a single file directly; javac compiles for larger projects.

Exercise

Write About.java that prints your name, school and favourite subject on three lines. Then pass your name as a command-line argument (java About.java Amina) and print it using args[0].

Show solution

Try the exercise yourself first — then compare your approach with this one.

Command-line arguments arrive in the args array. Checking args.length first avoids an ArrayIndexOutOfBoundsException when no name is given.

About.javaJava
public class About {
    public static void main(String[] args) {
        String name = args.length > 0 ? args[0] : "Amina";
        System.out.println("Name: " + name);
        System.out.println("School: Azania Secondary");
        System.out.println("Favourite subject: Biology");
    }
}
TerminalShell
java About.java Juma
# Name: Juma
# School: Azania Secondary
# Favourite subject: Biology

Check your understanding

  1. Where does every Java program start running?

  2. A file contains public class Report. What must the file be called?

  3. What does java Hello.java do?

  4. What does the JDK include that a JRE alone does not?

Ask AI