00:00

Introduction to Java – What Is Java and How Does It Work?

Last updated: 11 min read

Java is a general-purpose, object-oriented programming language that runs on billions of devices, from bank servers and Android phones to cloud services and smart cards. Its famous promise is "Write Once, Run Anywhere": you compile your code once, and the same compiled program runs on Windows, macOS, Linux or any other system that has a Java Virtual Machine.

This tutorial introduces Java for complete beginners: what it is, where it came from, how a Java program is compiled and run, its key features, its editions and versions, and what people build with it.

1. What Is Java?

Java is a high-level language, which means you write code in readable English-like statements instead of machine instructions. It is:

  • Object-oriented: programs are built from classes and objects that combine data and behaviour.
  • Platform-independent: code compiles to bytecode that runs on any Java Virtual Machine (JVM).
  • Statically typed: every variable has a type checked by the compiler, which catches many mistakes before the program runs.
  • Memory-managed: a garbage collector frees unused memory automatically.
  • Backed by a huge ecosystem: a rich standard library plus frameworks such as Spring Boot, Hibernate and Jakarta EE.

Java is sometimes described as "not 100% object-oriented" because it also has primitive types such as int and double, which are not objects. Those are kept for speed.

2. Your First Java Program

Every Java application starts from a main method inside a class:

HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        System.out.println("Welcome to Java.");
    }
}
Output
Hello, World!
Welcome to Java.
  • public class HelloWorld declares a class; the file must be named HelloWorld.java.
  • public static void main(String[] args) is the entry point the JVM calls.
  • System.out.println() prints a line of text.

The tutorial on how to write a Java program explains each word in detail.

3. A Short History of Java

James Gosling, the creator of Java
James Gosling, the creator of Java. Photo: Peter Campbell, CC BY-SA 4.0, via Wikimedia Commons.
YearMilestone
1991James Gosling, Mike Sheridan and Patrick Naughton start the "Green Project" at Sun Microsystems. The language is first called Oak, after a tree outside Gosling's office.
1995Renamed Java (after Java coffee) and announced publicly. Web browsers start running Java applets.
1996JDK 1.0 is released.
1998Java 2 (J2SE 1.2) adds the Collections Framework and Swing.
2004Java 5 adds generics, enums, annotations and the enhanced for loop.
2006–2007Sun releases Java as open source (OpenJDK).
2010Oracle acquires Sun Microsystems and becomes Java's steward.
2014Java 8 adds lambdas and the Stream API, a major modernisation.
2017Java moves to a new feature release every six months.
2021–2025Long-term-support releases Java 17, 21 and 25 add records, sealed classes, pattern matching and virtual threads.

James Gosling is known as the father of Java. The language was originally designed for interactive television, which turned out to be ahead of its time, before finding its place on the web and then on servers.

4. How a Java Program Runs

Java uses both a compiler and a virtual machine:

  1. Write source code in a .java file.
  2. Compile it with javac. The compiler checks the code and produces bytecode in a .class file. Bytecode is not tied to any processor.
  3. Run it with java. The JVM loads the bytecode, verifies it for safety, and executes it, first by interpreting it and then by compiling frequently used parts into fast native machine code with the JIT (Just-In-Time) compiler.
Terminal
javac HelloWorld.java   # creates HelloWorld.class (bytecode)
java HelloWorld         # the JVM runs the bytecode

# Since Java 11, a single-file program can also be run directly:
java HelloWorld.java

Because every operating system has its own JVM, the same .class file runs everywhere. That is the secret behind "Write Once, Run Anywhere". The roles of the JDK, JRE and JVM are explained in JDK vs JRE vs JVM.

5. Java Is Object-Oriented

In Java, you model real things as classes and create objects from them. This bank account keeps its balance private and changes it only through its methods:

BankAccount.java
// A class is a blueprint; objects are built from it
public class BankAccount {
    private final String owner;
    private double balance;

    public BankAccount(String owner, double openingBalance) {
        this.owner = owner;
        this.balance = openingBalance;
    }

    public void deposit(double amount) {
        if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
        balance += amount;
    }

    public void withdraw(double amount) {
        if (amount > balance) {
            System.out.println("Insufficient funds for " + owner);
            return;
        }
        balance -= amount;
    }

    public double getBalance() { return balance; }

    public static void main(String[] args) {
        BankAccount asha = new BankAccount("Asha", 1000);
        asha.deposit(500);
        asha.withdraw(2000);
        asha.withdraw(300);
        System.out.println("Asha's balance: " + asha.getBalance());
    }
}
Output
Insufficient funds for Asha
Asha's balance: 1200.0

The four OOP principles, encapsulation, inheritance, polymorphism and abstraction, are covered in the OOPs concept tutorials.

6. Key Features of Java

FeatureWhat it means
SimpleFamiliar C-style syntax without pointers or manual memory management.
Object-orientedCode is organised into classes and objects.
Platform-independentBytecode runs on any JVM.
SecureBytecode verification, no pointer arithmetic, and a managed runtime.
RobustStrong typing, exception handling and garbage collection.
MultithreadedBuilt-in support for running tasks in parallel, including lightweight virtual threads (Java 21).
High performanceThe JIT compiler turns hot code into optimised machine code.
DistributedRich networking APIs for building connected applications.

Each feature is explained with examples in Features of Java.

7. Modern Java at a Glance

Java has changed a lot since its early days. Here are a few modern features in one small program:

ModernJava.java
import java.util.List;

public class ModernJava {
    // Records (Java 16+): a compact class for plain data
    record Order(String item, int quantity, double price) {
        double total() { return quantity * price; }
    }

    sealed interface Shape permits Circle, Square { }
    record Circle(double radius) implements Shape { }
    record Square(double side) implements Shape { }

    // Pattern matching for switch (Java 21)
    static double area(Shape s) {
        return switch (s) {
            case Circle c -> Math.PI * c.radius() * c.radius();
            case Square q -> q.side() * q.side();
        };
    }

    public static void main(String[] args) {
        var orders = List.of(new Order("pen", 3, 10.0), new Order("book", 1, 250.0));
        double sum = orders.stream().mapToDouble(Order::total).sum();   // Streams (Java 8+)
        System.out.println("Order total: " + sum);

        System.out.printf("Circle area: %.2f%n", area(new Circle(1)));
        System.out.println("Square area: " + area(new Square(3)));

        String json = """
                {"language": "Java", "since": 1995}""";             // text block (Java 15+)
        System.out.println(json);
    }
}
Output
Order total: 280.0
Circle area: 3.14
Square area: 9.0
{"language": "Java", "since": 1995}

Records, sealed interfaces, pattern matching for switch, var, streams and text blocks all make modern Java code shorter and clearer.

8. Java Editions

EditionUsed for
Java SE (Standard Edition)The core language and libraries; desktop apps, tools and the base for everything else.
Jakarta EE (formerly Java EE)Enterprise web and server applications: servlets, persistence, messaging.
Java ME (Micro Edition)Small and embedded devices.
Java CardSmart cards and SIM cards.

9. Java Versions and LTS Releases

Since 2017, a new Java version is released every six months, in March and September. Every few releases is a Long-Term Support (LTS) version that companies use for years:

LTS versionReleasedHighlights
Java 82014Lambdas, Stream API, new Date-Time API
Java 112018var in lambdas, HTTP Client, running single-file programs
Java 172021Records, sealed classes, text blocks, pattern matching for instanceof
Java 212023Virtual threads, pattern matching for switch, record patterns, sequenced collections
Java 252025Compact source files, module imports, flexible constructor bodies, scoped values

For learning and new projects, use the latest LTS release. The examples on this site run on Java 21 or later.

10. Where Is Java Used?

  • Back-end and web services: Spring Boot, Jakarta EE, Quarkus and Micronaut power APIs for banks, retailers and streaming services.
  • Android apps: Android's APIs are Java-based, and Java runs alongside Kotlin.
  • Big data: Hadoop, Kafka, Spark (on the JVM), Elasticsearch and Cassandra are written in Java or JVM languages.
  • Enterprise systems: banking, insurance, trading and e-commerce platforms.
  • Desktop and tools: IntelliJ IDEA, Eclipse and many developer tools.
  • Cloud and microservices: containers, serverless functions and native images with GraalVM.
  • Embedded and IoT devices, and even Minecraft (Java Edition).

11. Advantages and Limitations

AdvantagesLimitations
Runs on any platform with a JVMUses more memory than C or C++ programs
Huge ecosystem and communityStart-up time is longer than native programs (improved by newer JVMs and GraalVM)
Strong typing catches errors earlyMore verbose than languages such as Python, though modern features help
Automatic memory managementLess direct control over hardware than C
Excellent performance for long-running serversGarbage collection pauses must be tuned for very low-latency systems

12. What to Learn Next

  1. Install Java and set up your environment.
  2. Understand JDK, JRE and JVM.
  3. Learn data types, variables and operators.
  4. Practise conditions and loops.
  5. Move on to classes and objects and the OOP concepts.

13. Summary

  • Java is a high-level, object-oriented, statically typed language created at Sun Microsystems and released in 1995–96.
  • Source code compiles to bytecode, which the JVM runs on any platform.
  • Key strengths are portability, safety, performance and a huge ecosystem.
  • New versions arrive every six months; LTS versions (8, 11, 17, 21, 25) are used in production.
  • Java powers back-end services, Android, big-data systems and enterprise software.

Reference: dev.java: Learn Java