Skip to main content

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
note

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.

YearVersionKey Events
1991OakJames Gosling starts the 'Oak' project for embedded device control
1995Java 1.0Officially released as 'Java'; explosive popularity via browser Applets
1997Java 1.1Inner classes, JDBC, RMI introduced
2004Java 5Generics, annotations, enhanced for-loop, autoboxing introduced
2006Java 6Performance improvements, enhanced web service support
2010-Oracle acquires Sun Microsystems
2011Java 7try-with-resources, diamond operator introduced
2014Java 8 LTSLambda expressions, Stream API, Optional, new Date/Time API (major paradigm shift)
2017Java 9Module system (JPMS), 6-month release cadence begins
2018Java 11 LTSHTTP Client API, expanded var keyword, Oracle JDK becomes free
2021Java 17 LTSSealed classes, pattern matching, record classes finalized
2023Java 21 LTSVirtual Threads, sequenced collections, record patterns
tip

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 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
ComponentRoleAudience
JVMTranslates bytecode into OS-native machine code and executes itCore Java runtime engine
JREJVM + standard libraries (enables running Java programs)End users of Java programs
JDKJRE + development tools (javac, javadoc, jar, etc.)Java developers
warning

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.

AreaStoresScope
HeapObjects created with new, arraysShared across all threads
StackMethod call frames, local variables, parametersPer-thread independent
Method AreaClass structure, static variables, constant poolShared across all threads
PC RegisterAddress of the currently executing instructionPer-thread independent
Native Method StackNative (C/C++) method execution infoPer-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
  • 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​

FeatureJavaC++PythonKotlin
ParadigmOOPOOP + proceduralMulti-paradigmOOP + functional
Memory ManagementAutomatic (GC)ManualAutomatic (GC)Automatic (GC)
Type SystemStatic typingStatic typingDynamic typingStatic typing
Execution ModelJVM bytecodeNativeInterpretedJVM bytecode
PerformanceHighVery highModerateHigh
Learning CurveModerateHighEasyModerate
Primary Use CasesBackend, AndroidSystems, gamesData science, AIAndroid, backend
Job MarketVery broadBroadVery broadGrowing
note

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.
tip

Java Learning Roadmap:

  1. Basic syntax (variables, conditionals, loops) β†’ Ch 1-3
  2. Object-oriented concepts (classes, inheritance, interfaces) β†’ Ch 4-6
  3. Core APIs (collections, streams, exception handling) β†’ Ch 7-10
  4. 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​

TopicDetail
Origin1995, Sun Microsystems
PhilosophyWrite Once, Run Anywhere
ExecutionBytecode on the JVM
Latest LTSJava 21 (2023)
Main UsesBackend servers, Android, big data
Key FeaturesOOP, platform independence, automatic memory management