Ch 1.1 Introduction to Java
Target Version: Java 17 / 21 (LTS) Official Docs: Oracle Java Documentation
Java is an object-oriented programming language first released in 1995 by Sun Microsystems. Currently maintained by Oracle, it is widely used across enterprise servers, Android apps, big data systems, and many other domains.
1. What is Java?β
Java was designed around the philosophy of "Write Once, Run Anywhere"β code written once can run on any platform. This is made possible because Java bytecode runs on the JVM (Java Virtual Machine) rather than on specific hardware.
When you write Java code (.java files), the compiler converts it to bytecode (.class files), which any JVM β tailored to the underlying OS β can execute. This means a program written on Windows runs identically on Linux or macOS.
Source code (.java) β javac compiler β Bytecode (.class) β JVM β Execution
Java is widely regarded as striking an excellent balance between performance, stability, and productivity. It consistently ranks among the most in-demand languages in the global job market.
2. Java Version Historyβ
Java has a mature history spanning over 30 years.
| Year | Version | Key Events |
|---|---|---|
| 1991 | Oak | James Gosling starts the 'Oak' project for embedded device control |
| 1995 | Java 1.0 | Officially released as 'Java'; explosive popularity via browser Applets |
| 1997 | Java 1.1 | Inner classes, JDBC, RMI introduced |
| 2004 | Java 5 | Generics, annotations, enhanced for-loop, autoboxing introduced |
| 2006 | Java 6 | Performance improvements, enhanced web service support |
| 2010 | - | Oracle acquires Sun Microsystems |
| 2011 | Java 7 | try-with-resources, diamond operator introduced |
| 2014 | Java 8 LTS | Lambda expressions, Stream API, Optional, new Date/Time API (major paradigm shift) |
| 2017 | Java 9 | Module system (JPMS), 6-month release cadence begins |
| 2018 | Java 11 LTS | HTTP Client API, expanded var keyword, Oracle JDK becomes free |
| 2021 | Java 17 LTS | Sealed classes, pattern matching, record classes finalized |
| 2023 | Java 21 LTS | Virtual Threads, sequenced collections, record patterns |
LTS (Long Term Support) versions receive guaranteed long-term support and are the preferred choice in production environments. Java 21 LTS is the latest LTS version and is recommended for learning.
3. Key Features by Major LTS Versionβ
Java 8 (2014) β A Paradigm Shiftβ
Java 8 introduced functional programming concepts on a large scale, marking a turning point in Java's evolution.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Java8Features {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Before Java 8
List<Integer> evensBefore = new java.util.ArrayList<>();
for (int n : numbers) {
if (n % 2 == 0) evensBefore.add(n);
}
// Java 8: Lambda + Stream
List<Integer> evens = numbers.stream()
.filter(n -> n % 2 == 0) // filter even numbers
.collect(Collectors.toList());
System.out.println("Even numbers: " + evens); // [2, 4, 6, 8, 10]
}
}
Key features of Java 8:
- Lambda Expressions
- Stream API
- Optional class
- New Date/Time API (java.time package)
- Interface default methods
Java 11 (2018) β Modern APIs Take Shapeβ
public class Java11Features {
public static void main(String[] args) {
// Improved String API
String text = " Hello, Java! ";
System.out.println(text.strip()); // "Hello, Java!" (trims whitespace)
System.out.println(text.isBlank()); // false
System.out.println("".isBlank()); // true
// Multiline string repetition
"Java\n".repeat(3).lines()
.forEach(System.out::println);
}
}
Java 17 (2021) β Records and Pattern Matchingβ
// record: concise definition of immutable data classes
record Point(int x, int y) {}
// sealed class: restrict which classes can extend/implement
sealed interface Shape permits Circle, Rectangle {}
record Circle(double radius) implements Shape {}
record Rectangle(double width, double height) implements Shape {}
public class Java17Features {
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println("x=" + p.x() + ", y=" + p.y()); // x=3, y=4
Shape shape = new Circle(5.0);
// Pattern matching (instanceof)
if (shape instanceof Circle c) {
System.out.println("Circle radius: " + c.radius());
}
}
}
Java 21 (2023) β Virtual Threads and the Futureβ
public class Java21Features {
public static void main(String[] args) throws InterruptedException {
// Virtual Thread: lightweight thread for massive concurrency
Thread vThread = Thread.ofVirtual().start(() -> {
System.out.println("Running in virtual thread: " + Thread.currentThread());
});
vThread.join();
// Sequenced collections: ordered collection interface
var list = new java.util.ArrayList<>(java.util.List.of("a", "b", "c"));
System.out.println("First: " + list.getFirst()); // a
System.out.println("Last: " + list.getLast()); // c
}
}
4. Core Characteristics of Javaβ
4.1 Object-Oriented Programming (OOP)β
Java is a fully object-oriented language β everything is centered around classes and objects.
// Class: the blueprint for an object
class Dog {
String name; // attribute (field)
int age;
// Constructor
Dog(String name, int age) {
this.name = name;
this.age = age;
}
// Behavior (method)
void bark() {
System.out.println(name + " barks: Woof!");
}
}
public class OopExample {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", 3); // create object
dog.bark(); // Buddy barks: Woof!
}
}
4.2 Platform Independenceβ
The same .class file runs on any OS as long as the JVM is installed.
4.3 Automatic Memory Management (Garbage Collection)β
Unlike C/C++, Java developers do not need to manually free memory. The JVM's Garbage Collector (GC) automatically removes objects that are no longer referenced.
4.4 Multithreading Supportβ
Java supports multithreaded programming at the language level.
4.5 Strong Type Systemβ
Java enforces strict type checking at compile time, preventing many runtime errors before execution.
int number = 42;
String text = "Hello";
// number = text; // Compile error! Type mismatch
5. JDK vs JRE vs JVMβ
Three terms that appear frequently in the Java ecosystem β understanding the distinctions is important.
βββββββββββββββββββββββββββββββββββββββββββ
β JDK β
β βββββββββββββββββββββββββββββββββββββ β
β β JRE β β
β β βββββββββββββββββββββββββββββββ β β
β β β JVM β β β
β β β (Bytecode execution engine)β β β
β β βββββββββββββββββββββββββββββββ β β
β β + Standard libraries (rt.jar) β β β
β βββββββββββββββββββββββββββββββββββββ β
β + javac, javadoc, jar, and dev tools β
βββββββββββββββββββββββββββββββββββββββββββ
| Component | Role | Audience |
|---|---|---|
| JVM | Translates bytecode into OS-native machine code and executes it | Core Java runtime engine |
| JRE | JVM + standard libraries (enables running Java programs) | End users of Java programs |
| JDK | JRE + development tools (javac, javadoc, jar, etc.) | Java developers |
Since Java 11, the JRE is no longer distributed separately. Both developers and end users must install the JDK.
6. JVM Internal Architectureβ
The JVM is not just a simple execution engine β it is a sophisticated virtual machine composed of multiple subsystems.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β JVM β
β β
β ββββββββββββββββ ββββββββββββββββββββββββββββββ β
β β Class Loader βββββΆβ Runtime Data Area β β
β ββββββββββββββββ β β β
β β ββββββββ ββββββββββββββ β β
β ββββββββββββββββ β β Heap β β Stack β β β
β β Execution β β β(obj) β β(call stack)β β β
β β Engine β β ββββββββ ββββββββββββββ β β
β β ββββββββββββ β β ββββββββββββββββββββββββ β β
β β βInterpreterβ β β β Method Area β β β
β β ββββββββββββ β β β(class info, constants)β β β
β β ββββββββββββ ββββββ ββββββββββββββββββββββββ β β
β β β JIT β β ββββββββββββββββββββββββββββββ β
β β βCompiler β β β
β β ββββββββββββ β ββββββββββββββββββββββββββββββ β
β ββββββββββββββββ β Garbage Collector β β
β ββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
6.1 Class Loaderβ
Loads .class files into JVM memory in three stages:
- Bootstrap ClassLoader: Loads core JDK libraries (java.lang, etc.)
- Extension ClassLoader: Loads extension libraries
- Application ClassLoader: Loads developer-written classes
6.2 Runtime Data Areaβ
The memory space the JVM uses during program execution.
| Area | Stores | Scope |
|---|---|---|
| Heap | Objects created with new, arrays | Shared across all threads |
| Stack | Method call frames, local variables, parameters | Per-thread independent |
| Method Area | Class structure, static variables, constant pool | Shared across all threads |
| PC Register | Address of the currently executing instruction | Per-thread independent |
| Native Method Stack | Native (C/C++) method execution info | Per-thread independent |
6.3 JIT Compiler (Just-In-Time Compiler)β
Java initially executes bytecode line by line via the interpreter. When the JIT compiler identifies frequently executed code (hotspots), it compiles that code into native machine code and caches it. This allows Java to approach C/C++ performance for hot code paths.
6.4 Garbage Collectorβ
Automatically removes objects from Heap memory that are no longer referenced.
public class GcExample {
public static void main(String[] args) {
// Object created β allocated on the Heap
String s1 = new String("Hello");
String s2 = new String("World");
s1 = null; // The "Hello" object is now eligible for GC
System.gc(); // Request GC (not guaranteed to run immediately)
System.out.println(s2); // World
}
}
7. The Java Compilation Process in Detailβ
Step 1: Write source code
Hello.java
β
Step 2: Compile with javac (Java Compiler)
javac Hello.java
β Hello.class (bytecode generated)
β
Step 3: JVM loads the class
java Hello
β Class Loader reads Hello.class into memory
β
Step 4: Bytecode Verification
β Checks for invalid or dangerous instructions
β
Step 5: Execution (Execution Engine)
β Interpreter: executes bytecode line by line
β JIT Compiler: compiles hotspots to native code
β
Step 6: Output
Hello, Java LLC!
8. The Java Ecosystemβ
Java is far more than a language β it forms a vast ecosystem.
Backend Web Developmentβ
- Spring / Spring Boot: The most widely used Java web framework
- Jakarta EE (formerly Java EE): Enterprise standard platform
- Quarkus, Micronaut: Cloud-native lightweight frameworks
Android App Developmentβ
- Kotlin is the official Android language, but Java is fully supported
- Most of the Android SDK is written in Java
Big Data / Distributed Systemsβ
- Apache Hadoop: Distributed data processing platform
- Apache Kafka: High-performance message streaming platform
- Apache Spark: Large-scale data analytics engine
- Elasticsearch: Distributed search engine
Database / Searchβ
- JDBC: Java standard API for database connectivity
- Hibernate / JPA: Object-Relational Mapping (ORM) framework
Build Toolsβ
- Maven: XML-based build and dependency management
- Gradle: Modern build tool with Groovy/Kotlin DSL
9. Java vs Other Languagesβ
| Feature | Java | C++ | Python | Kotlin |
|---|---|---|---|---|
| Paradigm | OOP | OOP + procedural | Multi-paradigm | OOP + functional |
| Memory Management | Automatic (GC) | Manual | Automatic (GC) | Automatic (GC) |
| Type System | Static typing | Static typing | Dynamic typing | Static typing |
| Execution Model | JVM bytecode | Native | Interpreted | JVM bytecode |
| Performance | High | Very high | Moderate | High |
| Learning Curve | Moderate | High | Easy | Moderate |
| Primary Use Cases | Backend, Android | Systems, games | Data science, AI | Android, backend |
| Job Market | Very broad | Broad | Very broad | Growing |
Kotlin and Java: Kotlin runs on the JVM and is 100% interoperable with Java. While Kotlin is now the official Android language, it can leverage the full Java library ecosystem.
10. Why Learn Java?β
Job Marketβ
- A significant portion of backend servers at large companies are built on Spring (Java/Kotlin)
- Consistently ranks in the top tier on TIOBE, Stack Overflow Developer Survey, and similar indices
- High demand across Android development, fintech, government systems, and more
Foundation for Other Languagesβ
- Understanding Java's OOP concepts makes learning C#, Kotlin, and Swift much easier
- The strict type system trains better code quality and design thinking
- Concepts like compilation, JVM, and GC deepen computer science understanding
Rich Learning Resourcesβ
- 30+ years of history means abundant books, courses, blogs, and Stack Overflow answers
- A large, active community makes problem-solving easier
11. Your First Java Programβ
The best way to understand Java's basic structure is to write it yourself.
/**
* First Java Program - Hello World
* Demonstrates the basic structure of a Java program.
*/
public class HelloWorld {
/**
* Program entry point
* The JVM looks for this method when the program starts.
*
* @param args command-line argument array
*/
public static void main(String[] args) {
// Print text to the console (with newline)
System.out.println("Hello, Java!");
// Print current Java version
System.out.println("Java version: " + System.getProperty("java.version"));
// Print operating system info
System.out.println("OS: " + System.getProperty("os.name"));
}
}
Expected Output:
Hello, Java!
Java version: 21.0.2
OS: Windows 11
Code Structure Explainedβ
public class HelloWorld: Class declaration. Must match the filename (HelloWorld.java) exactly.public static void main(String[] args): Program entry point. This exact signature is required for the JVM to recognize it.System.out.println(...): Outputs text to the standard output stream.System.getProperty(...): Queries JVM system properties.
Java Learning Roadmap:
- Basic syntax (variables, conditionals, loops) β Ch 1-3
- Object-oriented concepts (classes, inheritance, interfaces) β Ch 4-6
- Core APIs (collections, streams, exception handling) β Ch 7-10
- Real-world projects (file I/O, networking, DB access) β Ch 11-15
Understanding each stage thoroughly and writing code hands-on is the key to mastering Java!
Summaryβ
| Topic | Detail |
|---|---|
| Origin | 1995, Sun Microsystems |
| Philosophy | Write Once, Run Anywhere |
| Execution | Bytecode on the JVM |
| Latest LTS | Java 21 (2023) |
| Main Uses | Backend servers, Android, big data |
| Key Features | OOP, platform independence, automatic memory management |