Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

JV
Java
0/14 completed
Beginner
Hello JavaVariables and typesControl flowMethodsArrays and strings
Intermediate
Classes and objectsInheritance and interfacesCollectionsExceptions
Advanced
GenericsLambdas and streamsRecords, enums and pattern matching
Pro
Concurrency and virtual threadsBuild tools, testing and real projects
Tracks / Java / Beginner

Hello Java

Java is one of the most widely used programming languages in the world. Banks, airlines, online shops, Android apps, big-data systems like Kafka and Spark, and Minecraft all run on Java. It's strongly typed, object-oriented, and famous for one idea: write once, run anywhere.

How Java runs

Java code isn't run directly. It goes through two steps:

Main.java  ──javac──▶  Main.class (bytecode)  ──java──▶  runs on the JVM
 (you write)           (portable, any OS)              (Windows, macOS, Linux…)
  1. The compiler (javac) checks your code and turns it into bytecode.
  2. The Java Virtual Machine (JVM) runs that bytecode on any operating system, optimising hot code into fast machine code as it runs.

Because the compiler checks types before the program runs, many mistakes are caught early: a big reason companies trust Java for large systems.

💡 About the examples in this track: Java can't run inside a web page the way Python and JavaScript can, so every example shows its Output underneath. Every program in this track was compiled and run with Java 25 to produce that output exactly. Copy any example (there's a Copy button) and run it yourself.

Your first program

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

Every part has a job:

Code Meaning
public class Main All Java code lives inside a class. The file must be named Main.java.
public static void main(String[] args) The entry point: where the program starts.
System.out.println(...) Print a line of text.
{ } Mark the start and end of blocks.
; Ends every statement.

It looks like a lot of ceremony for "Hello". You'll understand every word (public, static, void, String[]) by the end of the classes lessons. For now, think of it as the frame every program sits in.

Printing

java
public class Main {
    public static void main(String[] args) {
        System.out.println("Line one");
        System.out.print("No newline after this. ");
        System.out.print("Same line.\n");
        System.out.println("6 x 7 = " + (6 * 7));
        System.out.printf("%s is %d years old and %.1f m tall%n", "Asha", 31, 1.68);
        // This is a comment: the compiler ignores it
        /* So is this,
           across several lines */
    }
}
Output
Line one
No newline after this. Same line.
6 x 7 = 42
Asha is 31 years old and 1.7 m tall
  • println prints and moves to a new line; print doesn't.
  • + joins text and values.
  • printf formats values: %s text, %d whole number, %.1f a decimal with 1 place, %n newline.

Compile errors are your friend

Forget a semicolon and the compiler refuses to build the program, pointing at the exact spot:

java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello")
    }
}
Output
Main.java:3: error: ';' expected
        System.out.println("Hello")
                                   ^
1 error
error: compilation failed

In languages like Python and JavaScript, many mistakes only show up when that line runs. Java catches them before the program starts.

Installing Java

  1. Install a JDK (Java Development Kit). Pick a long-term support version: Java 25, 21 or 17. Free builds: Eclipse Temurin (adoptium.net), Microsoft, Amazon Corretto, Oracle OpenJDK.
    • macOS: brew install --cask temurin
    • Windows: the installer from adoptium.net, or winget install EclipseAdoptium.Temurin.25.JDK
    • Linux: sudo apt install openjdk-25-jdk (or the newest your distribution offers)
  2. Check it in a terminal:
bash
java -version
javac -version
  1. Run a program:
bash
javac Main.java      # compile → Main.class
java Main            # run

java Main.java       # or compile and run in one step (Java 11+)
  1. Use an IDE: IntelliJ IDEA (Community edition is free; the most popular Java IDE), VS Code with the "Extension Pack for Java", or Eclipse. They compile as you type and underline errors instantly.

A modern shortcut (Java 25)

Since Java 25, small programs can skip the class and the long main signature. This is great for learning and scripts:

java
void main() {
    IO.println("Hello from compact Java!");
    String name = "Asha";
    IO.println("Hi " + name);
}
Output
Hello from compact Java!
Hi Asha

Most real code (and the rest of this track) uses the classic form, because you'll see it everywhere: in every company codebase, tutorial and framework.

Java vs JavaScript

Despite the name, they're unrelated languages. The name was a 1990s marketing decision.

Java JavaScript
Typing Static (types checked when compiling) Dynamic
Runs on The JVM Browsers, Node.js
Typical use Backends, Android, enterprise, big data Websites, web apps, Node backends

Practice

On your computer (or any online Java compiler), write a program that prints:

  1. Your name on one line.
  2. Your age next year, calculated with +.
  3. A formatted line with printf: "Coffee costs ₹120.00".
✎ Check your understanding

1.What does javac do?

2.Why can the same compiled Java program run on Windows, macOS and Linux?

3.What must the file containing public class Main be called?