Chapter 1: Introduction to Java
Java is one of the few programming languages that has sustained its influence across generations of developers and computing platforms. First released in the mid-1990s, it has quietly become the backbone of modern software, from Android apps and enterprise servers to scientific systems and embedded devices. Its guiding promise, “write once, run anywhere,” remains more relevant than ever in a world of diverse hardware and cloud environments.
At its core, Java is about clarity, safety, and longevity. It was designed to combine the familiarity of C-style syntax with the discipline of object-oriented structure and the safety of automatic memory management. The result is a language that balances power with predictability: fast enough for large-scale systems, yet structured enough for long-term maintenance. Unlike languages that come and go with trends, Java continues to evolve deliberately, preserving compatibility while embracing features such as lambdas, streams, pattern matching, and modularity.
This book takes a practical and modern view of Java. It assumes you already understand basic programming ideas (variables, loops, functions) and want to become fluent in Java’s particular approach. We begin from first principles: installing the current Long-Term Support (LTS) release, writing and compiling your first program, and understanding how Java’s strict type system and virtual machine work together to make code both portable and robust.
The goal is not simply to list syntax rules but to help you see the language as a living design system; how its structure guides thought, how its type safety enforces clarity, and how its ecosystem supports everything from command-line utilities to cloud-native applications. Each chapter builds on the last, moving steadily from fundamentals to expressive features such as generics, concurrency, and the functional style introduced in modern Java.
By the end of this book you will have written and understood a broad range of examples that reflect how real Java is written today. You will also have the foundation to read, extend, and reason about almost any Java codebase with confidence. Whether you aim to master backend development, Android, data processing, or simply want a language that rewards disciplined thought, Java remains one of the most reliable paths to professional programming fluency.
Who this book is for
This book is written for anyone who already understands the basic ideas of programming and wants to gain a clear, complete understanding of how Java works. If you have coded before in another language such as Python, JavaScript, C, C++, or PHP, you will find the transition smooth. Many of the core concepts are familiar, but Java’s structure, typing system, and disciplined syntax give it a character of its own.
Java rewards clear thinking and organized design. It expects you to define your ideas precisely, but once you learn its rhythm, it helps you build programs that scale easily from small scripts to large applications. This book focuses on that balance between simplicity and structure, showing how to use Java’s features confidently, without getting lost in jargon or unnecessary complexity.
If you are new to programming, you will also find that Java is an excellent first language. Its strict rules about typing, scope, and structure help you develop good habits from the start, and its built-in libraries let you accomplish a great deal with concise, readable code. Each concept in this book is introduced gently, with examples that build from simple to practical, so you can follow along even if this is your first experience writing code.
Whether you are a student learning Java for coursework, a professional adding it to your skill set, or a self-taught programmer exploring a new platform, this book will guide you through the essentials of the language and its modern ecosystem. Every topic is explained in plain language, illustrated with short, focused examples, and connected to the bigger picture of how real programs are written and maintained.
What Java is and why it matters
Java is a high-level, general-purpose programming language created to make software portable, secure, and dependable across platforms. From its first release in 1995, its philosophy has been simple: write the code once, then run it anywhere a Java Virtual Machine (JVM) exists. That principle, known as platform independence, made Java the first language to achieve widespread, practical cross-platform development, an achievement that still defines its identity today.
Unlike languages that compile directly to machine code, Java compiles to bytecode, an intermediate form that runs inside the JVM. This approach lets the same program run unchanged on Windows, macOS, Linux, and many other systems. The JVM also manages memory automatically through garbage collection, reducing entire categories of programming errors and freeing developers to focus on logic rather than low-level housekeeping.
Another key reason Java has endured is its balance between simplicity and depth. The language was designed to be familiar to anyone who has used C-style syntax, yet it removed unsafe features like manual pointer manipulation and multiple inheritance of implementation. It enforces a clear object-oriented structure, so every piece of code lives within a class and follows predictable, readable patterns. As Java evolved, it gained modern features such as generics, annotations, lambda expressions, records, and modularity, extending its power while keeping its design consistent.
Today, Java runs on billions of devices and underpins much of the world’s software infrastructure. It powers enterprise systems, Android applications, data platforms, and cloud backends. Its standard library and enormous open-source ecosystem make it suitable for almost any domain, from small tools to distributed systems serving millions of users. Beyond its technology, Java’s greatest strength is its stability: code written years ago can often still compile and run with little or no modification.
For learners and professionals alike, Java represents a dependable skill that bridges theory and practice. Understanding Java means understanding how structured, strongly typed languages organize complexity. Once you know how Java works, you gain insight into a wide range of other modern languages that draw on its design.
Versions and setup
At the time of writing, the main Long-Term Support (LTS) version of Java is Java 25, representing the modern era of the language: stable, fully compatible with past code, and enhanced with new tools and syntax that simplify development. Unless you are maintaining legacy systems, you should begin with Java 25 (or newer) to take advantage of current language features and performance improvements.
Installing Java is straightforward. The most common distribution is the Open Java Development Kit (OpenJDK), which is completely free and maintained by the open-source community alongside vendors such as Oracle, Amazon, and Eclipse. You can download the latest LTS or current release for your platform directly from the official source: jdk.java.net
Each installation includes the Java compiler (javac) and runtime environment (java), which together let you compile and execute Java programs. Once installed, you can verify your setup from a terminal or command prompt:
java -version
javac -version
If both commands print version information, your environment is ready. You may also wish to install an Integrated Development Environment (IDE) such as one of those listed following. These tools provide syntax highlighting, autocompletion, project management, and integrated build support, all of which make development smoother and faster:
- IntelliJ IDEA: jetbrains.com/idea
- Eclipse: eclipseide.org
- VS Code (with Java extension pack): code.visualstudio.com/docs/java/extensions
javac) converts source files ending in .java into bytecode files ending in .class. The Java Virtual Machine (java) then executes those .class files, interpreting or just-in-time compiling them for your system’s processor.
Because the Java Platform is designed to be consistent across operating systems, your workflow will look nearly identical whether you use Windows, macOS, or Linux. You create your source file, compile it into bytecode, and run it under the JVM. The same steps apply regardless of where the program is executed, which is one reason Java became such a durable choice for cross-platform software.
Most developers also configure an environment variable named JAVA_HOME to point to their JDK installation directory. This makes it easier for build tools like Maven or Gradle to locate the compiler automatically. Your IDE can usually detect the JDK without manual setup, but defining JAVA_HOME ensures compatibility across different systems and command-line tools.
Compiling and running your first program
Once Java is installed and your environment is set up, you are ready to write your first program. Java code is written in plain text files that end with the .java extension. Each file contains one or more classes, and the file name must match the name of the public class it defines. This rule helps Java locate the correct entry point when compiling and running code.
To begin, create a new file named HelloJava.java in your working folder and open it in any text editor or IDE. Enter the following code:
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
This short program defines a class named HelloJava containing a single method called main. The main() method is special, since it is where every Java application begins execution. The line inside it calls System.out.println(), which prints a message followed by a line break to standard output.
To compile the program, open a terminal or command prompt in the same folder and run:
javac HelloJava.java
If there are no errors, the compiler produces a file named HelloJava.class in the same directory. This file contains the Java bytecode, an intermediate format executed by the Java Virtual Machine (JVM). To run the compiled program, type:
java HelloJava
You should see the following output:
Hello, Java!
When you run a program with the java command, you do not include the .class extension. The JVM automatically looks for that file and executes the compiled bytecode within it.
javac) translates source code into bytecode stored in .class files. Then, the Java Virtual Machine interprets or just-in-time compiles this bytecode into native instructions for your operating system and processor.
Many developers prefer to run Java programs directly from an IDE, where compilation and execution happen automatically with a single click. However, learning how to compile and run programs manually helps you understand how the toolchain works and ensures that you can always build and execute code from any environment.
javac or java commands are not recognized, check that your PATH and JAVA_HOME environment variables are set correctly. They must point to the bin folder inside your JDK installation.
Editing, formatting, and code style
Every programming language has its own visual rhythm, and Java’s is deliberate, structured, and designed for clarity. Following consistent formatting makes your code easier to read, debug, and share with others. Although the compiler ignores whitespace and indentation, human readers do not, so treat style as a form of communication.
Java source code is organized into classes, methods, and blocks, each enclosed in braces ({…}). Consistent indentation and spacing make these structures clear at a glance. The common convention is to use two (as in these examples) or four spaces for indentation (never tabs), with braces placed on their own lines or aligned with the opening statement. Both approaches are valid, but the key is consistency throughout your codebase.
// Example of standard Java/C style
public class Example {
public static void main(String[] args) {
int sum = 3 + 4;
System.out.println("The sum is " + sum);
}
}
// Example of less used Allman style
public class Example
{
public static void main(String[] args)
{
int sum = 3 + 4;
System.out.println("The sum is " + sum);
}
}
Class names use PascalCase (also known as UpperCamelCase), method and variable names use camelCase, and constants use UPPER_CASE. These patterns are followed across nearly all Java projects, making code predictable and familiar to other developers.
class DataProcessor {
static final int MAX_SIZE = 100;
private int itemCount;
public void addItem() {
itemCount++;
}
}
Line length should generally stay under about 100 characters, and blank lines can be used sparingly to separate logical sections of code. Comments begin with // for single lines, or /* … */ for multiple lines. For documentation, Java uses Javadoc comments (/** … */), which can be processed into formatted reference pages. For example:
/**
* Returns the larger of two numbers.
* @param a The first value
* @param b The second value
* @return The greater of a or b
*/
public static int max(int a, int b) {
return (a > b) ? a : b;
}
google-java-format, Prettier, and IDE-integrated formatters (for example, in IntelliJ IDEA or Eclipse) can automatically apply consistent indentation and layout. Let tools handle routine formatting so you can focus on logic and structure.
Modern Java development environments also perform linting, which is the automatic checking of potential issues like unused variables, missing documentation, or unsafe type conversions. These checks encourage clean, maintainable code and help you learn good practices as you go.