New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Java› Getting Started with Java

Getting Started with Java

💻 Java

Install a JDK, compile your first class and understand how Java runs.

Lesson 1 of 9 Tutorial
0 of 9 completed

Java source is compiled to bytecode, which runs on the Java Virtual Machine. That is why the same program runs on Windows, macOS and Linux.

By the end of this lesson you can:
  • Explain the JDK, JVM and bytecode.
  • Compile and run a class.
  • Read a simple Java program.

Install a JDK

Install a Java Development Kit such as Temurin or Oracle JDK. The JDK includes the compiler javac and the runtime java.

javac -version
java -version

Your First Program

The file name must match the public class name. Every Java program starts from main.

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

Compile and Run

Compile to a .class file, then run the class name without the extension.

javac Hello.java
java Hello
Tip Java is case-sensitive. System with a capital S is not the same as system.

Practice

  1. Print your name and school on separate lines.
  2. Explain the difference between javac and java.
  3. Compile and run a program from the command line.

Hands-on exercise

Write a class called AboutMe that prints:
1. Your name.
2. Your favourite subject.
3. One sentence about why you are learning Java.
Compile and run it.

Recap

Java compiles to bytecode, the JVM runs it and main is the entry point.

Example

javac -version
java -version

Exercises

Exercise 1. Hello, Java Easy
Write a class called AboutMe that prints:
1. Your name.
2. Your favourite subject.
3. One sentence about why you are learning Java.
Compile and run it.
Java exercise-1.java
Expected Output
Three lines printed in order.

Lesson Nav

Lesson sections