← Chapter 1 — Getting Started with Java

2. Installing Java & Your First Program

mixed

Description

Install a Java Development Kit (JDK), not only a Java runtime. The JDK includes the compiler and tools needed to create programs.

An editor or IDE can speed up writing code, but learning the compile-and-run commands is useful. It makes the relationship between a .java file and a running program clear.

Subtopics

JDK and PATH

Description

The JDK supplies javac and java.

Explanation

Adding the JDK bin folder to PATH lets the terminal locate those commands.

Example

java --version

Compile then run

Description

Use two separate commands at first.

Explanation

The class name after java has no .java extension.

Example

javac FirstProgram.java
java FirstProgram

Explanation

A public class must use the same name as its file. If the file is FirstProgram.java, the public class must be FirstProgram; letter case matters.

Example

public class FirstProgram {
    public static void main(String[] args) {
        String learner = "Rohan";
        System.out.println("Java is ready for " + learner);
    }
}

Output

Java is ready for Rohan

Key points

  • Use a JDK to build Java programs.
  • Public class and file names must match.
  • javac compiles and java runs bytecode.

Video

Open video