Notifications

No notifications

/Phase 1

Introduction to Java

Welcome to Java ☕

Java is a statically typed, object-oriented, compiled-to-bytecode language created by James Gosling at Sun Microsystems in 1995. The slogan "Write Once, Run Anywhere" still holds: your code compiles to bytecode, which runs on the JVM (Java Virtual Machine) on Windows, macOS, Linux, Android — anywhere.

Where Java Runs Today

DomainWhy Java
Android appsNative first language (alongside Kotlin)
Big enterprise / banksStable, mature, vast ecosystem (Spring, Jakarta EE)
Backend servicesHigh throughput, mature tooling, strong typing
Big dataHadoop, Spark, Kafka — all JVM
EmbeddedSmart cards, IoT (Java SE Embedded)

Your First Java Program

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

Save as Hello.java (filename must match the public class). Then:

javac Hello.java     # compile to Hello.class (bytecode)
java  Hello          # run on the JVM

> Java 11+ also lets you run a single file directly: java Hello.java (no compile step).

JDK vs JRE vs JVM

TermWhat it is
JVMThe runtime that executes .class bytecode
JREJVM + standard library (just runs Java apps)
JDKJRE + compiler (javac) + tools (jshell, jdeps, javadoc)

You install the JDK to develop. Get OpenJDK 21 LTS from [adoptium.net](https://adoptium.net/).

On this page

Detailed Theory

Java was designed to fix C++'s biggest pains — manual memory management, header files, fragile cross-platform builds — while keeping a familiar C-style syntax. Trade-off: a fatter runtime and slower startup. Worth it for most server/Android workloads.

A Brief History

YearReleaseHighlight
1995Java 1.0Released by Sun
2004Java 5Generics, autoboxing, enhanced for, annotations
2014Java 8Lambdas + Streams — biggest shift since 1.0
2017Java 9Modules (JPMS), JShell
2018Java 11LTS; var (Java 10), HTTP client
2021Java 17LTS; sealed classes, records, pattern matching
2023Java 21LTS; virtual threads, pattern matching for switch

Use Java 21 (LTS) for new projects. Java 17 is also widely deployed.

How Java Code Becomes Output

.java  →  javac  →  .class  →  JVM (interprets / JIT-compiles)  →  CPU instructions

The JVM uses a JIT (Just-In-Time) compiler to convert hot bytecode into native code at runtime — so long-running Java apps approach C++ speed.

Anatomy of a Java File

package com.example.demo;        // optional but recommended

import java.util.List; // standard library import

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

Rules

  • Filename must match the public class name: Hello.javapublic class Hello.
  • The entry point is exactly public static void main(String[] args).
  • Statements end with semicolons ;.
  • Blocks use braces { }.
  • Strings use double quotes ". Single quotes ' are only for char.

Installing the JDK

# Windows (winget)
winget install EclipseAdoptium.Temurin.21.JDK

# macOS (Homebrew) brew install --cask temurin@21

# Linux (Debian/Ubuntu) sudo apt install openjdk-21-jdk

Verify:

java --version
javac --version

Both should report 21.x.

Running Java

The Long Way (any version)

javac Hello.java
java  Hello

Single-File (Java 11+)

java Hello.java

JShell — Java's REPL (Java 9+)

$ jshell
jshell> int x = 5;
jshell> System.out.println(x * x);
25
jshell> /exit

Great for trying snippets without writing a full class.

A Slightly Bigger Example

public class Greet {
    public static void main(String[] args) {
        String name = args.length > 0 ? args[0] : "world";
        System.out.println("Hello, " + name + "!");
    }
}

$ java Greet.java Asha
Hello, Asha!

String[] args is the array of command-line arguments — note it's a real String array, not argc/argv.

Build Tools You'll Meet

For real projects you'll use a build tool, not raw javac:

ToolNotes
MavenXML config (pom.xml); industry default for libraries
GradleGroovy/Kotlin DSL; preferred by Android & many startups

For learning, plain javac/java or an IDE is enough.

IDEs

  • IntelliJ IDEA Community — free; the best Java experience by a wide margin.
  • VS Code + Extension Pack for Java — lightweight, great for small projects.
  • Eclipse — older, still common in enterprises.

Java vs C++ vs Python (Quick Map)

JavaC++Python
MemoryGCManual / RAIIGC
SpeedFast (JIT)FastestSlowest
StartupSlow JVM warmupInstantFast
TypesStaticStaticDynamic
Use casesBackend, AndroidSystems, gamesScripts, ML

Common Beginner Trip-Ups

MistakeFix
Filename ≠ public class nameRename file to match
Forgot semicolonJava is C-family — every statement ends with ;
Used ' for a stringStrings are "..."; 'x' is one char
Missing public static void main exactlyEven String args[] works, but order matters
== to compare stringsUse .equals() for String content equality

What's Next

You now know how to write, compile and run a Java program. Next: variables and the type system that catches most errors before the program even starts.