Chapter 1: Introduction to Java

Welcome to Java! Developed by Sun Microsystems in 1995, Java is one of the most widely used programming languages in the world. Its core philosophy, "Write Once, Run Anywhere" (WORA), is made possible by the Java Virtual Machine (JVM), which allows Java programs to run on any device that supports Java, regardless of the underlying hardware architecture.

Why Java?

Java is the gold standard for enterprise-level applications, Android mobile development, and large-scale web backends. It is a strictly object-oriented, class-based language, which forces you to write structured, maintainable, and scalable code. Even today, with newer languages emerging, Java's massive ecosystem, libraries, and reliability make it an essential skill for any software developer.

Understanding the Java Ecosystem

Unlike C or C++, Java code is not compiled directly into machine code. Instead, it is compiled into Bytecode. This bytecode is then executed by the JVM. This extra layer provides significant security features and cross-platform compatibility, making Java a favorite for secure, high-traffic systems.

Your First Java Program

In Java, every single line of code must live inside a class. This is the ultimate expression of the Object-Oriented paradigm.

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

Crucial Concepts

Java manages memory automatically via a Garbage Collector. This means you don't have to worry about manual memory allocation or freeing pointers, which is a massive relief compared to C or C++. However, this also means you need to be mindful of how you create objects to avoid performance bottlenecks.

Try It Yourself

  1. Install the Java Development Kit (JDK) on your machine.
  2. Create a file named HelloWorld.java and compile it using javac HelloWorld.java.
  3. Run your compiled code using java HelloWorld.

"Write Once, Run Anywhere"

Java's famous slogan reflects its core design: Java code compiles into bytecode that runs on the Java Virtual Machine (JVM), so the same program runs on Windows, Mac, or Linux without changes. This portability, combined with strong backward compatibility, is why Java remains the backbone of Android apps, enterprise banking systems, and large-scale server applications decades after its release in 1995.

Anatomy of a Java Program

Every Java application needs at least one class, and execution always begins at the main method — the entry point the JVM looks for first.

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

Notice that the file must be named HelloWorld.java, matching the public class name exactly — Java enforces this strictly.

Compiling and Running Java

Java code is first compiled with javac HelloWorld.java, which produces a HelloWorld.class bytecode file, then executed with java HelloWorld. This two-step process is different from Python's direct interpretation and is part of what makes Java programs run efficiently across platforms.