The Java Ecosystem

Chapter 1. Architecture

Q1: The JVM (Java Virtual Machine)

"Write Once, Run Anywhere" is achieved through the JVM. Unlike C++ which compiles to machine code specific to an OS, Java compiles to Bytecode (`.class` files).

The JVM creates an abstraction layer between the code and the hardware. It loads code, verifies it, executes it, and manages the runtime environment.

Source Code (.java) -> Compiler (javac) -> Bytecode (.class) -> JVM -> Machine Code (0101...)

Q2: Memory Management & GC

Java automates memory management using a Garbage Collector (GC). Developers don't manually `malloc` or `free`.

Q2.1. Heap vs Stack

Stack: Stores method calls, local variables, and reference variables. Fast access, LIFO structure.
Heap: Stores Objects and JRE classes. All objects created with `new` live here.

Q2.2. Garbage Collection

The GC identifies objects that are no longer reachable from the "GC Roots" (like active stack frames) and reclaims their memory.
Modern algorithms like G1 (Garbage First) and ZGC minimize "Stop-The-World" pauses, making Java suitable for low-latency applications.

Chapter 2. Paradigm

Q3: Object-Oriented Programming (OOP)

Java is strictly Object-Oriented (mostly). Everything revolves around Objects and Classes.

  • Encapsulation: Hiding internal state (private fields, public getters/setters).
  • Inheritance: Code reuse via `extends`. `Item -> Book`.
  • Polymorphism: One interface, many implementations. `List` can be `ArrayList` or `LinkedList`.
  • Abstraction: Hiding complexity using `interface` and `abstract class`.

Q4: Functional Java (Streams)

Since Java 8, the language has embraced functional programming concepts. The Streams API allows for declarative data processing.

// Imperative
List<String> filtered = new ArrayList<>();
for (String s : list) {
    if (s.startsWith("A")) filtered.add(s);
}

// Functional (Streams)
List<String> filtered = list.stream()
    .filter(s -> s.startsWith("A"))
    .toList();

Chapter 3. The Spring Framework

Q5: Spring & Spring Boot

Inversion of Control (IoC): Instead of you creating objects (`new Service()`), the Spring Container creates them and "injects" them where needed (`@Autowired`).

Spring Boot: An opinionated view of the Spring platform. It handles configuration automatically. You don't configure a Tomcat server; you just "run" the application, and it comes with an embedded server.

@RestController
public class HelloController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}