Java and JVM — Comprehensive Notes
1. History of Java
➤ Origin
Java was developed by James Gosling, Mike Sheridan, and Patrick Naughton at Sun
Microsystems in 1991.
The original project was called "Oak", named a er an oak tree outside Gosling’s office.
Oak was designed for consumer electronic devices like set-top boxes, microwaves, and
TV remotes.
➤ Renaming and Launch
In 1995, Oak was renamed to Java because “Oak” was already a trademark by another
company.
The name "Java" was inspired by Java coffee, a type of coffee from Indonesia.
The first official version of Java (Java 1.0) was released by Sun Microsystems in 1995.
➤ Acquisi on by Oracle
In 2010, Oracle Corpora on acquired Sun Microsystems, taking over Java's development
and licensing.
2. Why Java Was Created (Mo va on and Purpose)
➤ Core Reasons:
1. Pla orm Independence
o Java programs are compiled to an intermediate form called bytecode, which can
run on any device with a JVM.
o This gave birth to Java’s philosophy: “Write Once, Run Anywhere” (WORA).
2. Internet-Centric Programming
o Java was created during the rise of the Internet.
o It was designed to build networked applica ons with built-in support for HTTP,
TCP/IP, sockets, etc.
3. Security
o Java has a sandbox execu on model for running untrusted code (like in
browsers).
o It restricts access to certain system resources unless explicitly permi ed.
4. Robust and Reliable
o Java emphasizes strong memory management, type checking, excep on
handling, and no pointers, reducing system crashes.
5. Object-Oriented Programming
o Java enforces OOP principles such as inheritance, polymorphism, encapsula on,
and abstrac on, encouraging modular, reusable code.
6. Mul threading Support
o Java has built-in concurrency with threads, making it easy to write mul threaded
programs.
7. Simplicity and Familiarity
o Syntax similar to C/C++, but removes complex features like operator overloading
and mul ple inheritance using classes.
8. Performance (with JIT)
o Java balances performance and flexibility, becoming faster over me with Just-
In-Time (JIT) Compila on.
3. Java Programming Language Structure
Example Code:
java
CopyEdit
public class HelloWorld {
public sta c void main(String[] args) {
System.out.println("Hello, World!");
Key Elements:
Class: A blueprint for objects. Java programs are made of classes.
Method: A block of code that performs a specific task. main() is the entry point.
Object: An instance of a class.
Package: A namespace that organizes classes and interfaces.
4. JVM (Java Virtual Machine)
➤ What is JVM?
JVM is a part of the Java Run me Environment (JRE).
It is an abstract compu ng machine that enables a computer to run Java bytecode.
➤ Key Responsibili es of JVM:
Loading Java class files.
Verifying the code for security and consistency.
Execu ng the bytecode.
Memory Management (via Garbage Collector).
Providing Run me Environment for Java apps.
5. JVM Architecture — In Depth
┌─────────────────────┐
│ Class Loader │
└─────────────────────┘
┌─────────────────────┐
│ Run me Data Areas │
└─────────────────────┘
┌─────────────────────┐
│ Execu on Engine │
└─────────────────────┘
┌─────────────────────┐
│ Na ve Interface │
└─────────────────────┘
┌─────────────────────┐
│ Na ve Libraries │
└─────────────────────┘
5.1 Class Loader Subsystem
Responsible for loading class files (.class), verifying, and preparing them for execu on.
Phases of Class Loading:
1. Loading – Reads .class files.
2. Linking:
o Verifica on: Checks bytecode format and validity.
o Prepara on: Allocates memory for sta c variables.
o Resolu on: Converts symbolic references to actual memory references.
3. Ini aliza on – Executes sta c ini alizers.
5.2 Run me Data Areas
➤ 1. Method Area
Stores class structure (methods, fields, constant pool).
Shared across all threads.
➤ 2. Heap
Run me storage for objects and arrays.
Shared memory across threads.
Managed by Garbage Collector.
➤ 3. Java Stack
Each thread has its own stack.
Stores method call frames, local variables, operand stacks.
Grows and shrinks as methods are invoked and return.
➤ 4. PC (Program Counter) Register
Each thread has a Program Counter Register.
Holds the address of the current execu ng instruc on.
➤ 5. Na ve Method Stack
Executes na ve (non-Java) code using C/C++ libraries.
Used when Java interacts with the opera ng system via JNI.
5.3 Execu on Engine
The heart of JVM — responsible for running the bytecode.
Components:
1. Interpreter
o Executes bytecode line-by-line.
o Simple but slower.
2. JIT (Just-In-Time) Compiler
o Compiles hot (frequently executed) bytecode into na ve machine code for be er
performance.
o Results in faster execu on a er ini al interpreta on.
3. Garbage Collector
o Automa cally deallocates memory for unreachable objects.
o Uses algorithms like Mark and Sweep, Genera onal GC, G1 GC.
5.4 Na ve Interface (JNI)
Java Na ve Interface is a bridge for Java code to call or be called by na ve applica ons
(C/C++).
Useful for:
o Interac ng with low-level system libraries.
o Legacy integra on.
5.5 Na ve Libraries
Libraries wri en in C/C++ (e.g., libjava.so, java.dll).
Pla orm-dependent.
6. Java Program Lifecycle (Execu on Process)
1. Wri ng Code: Developer writes .java files.
2. Compila on: The Java compiler (javac) compiles code into .class files (bytecode).
3. Class Loading: Class Loader loads .class into JVM.
4. Verifica on: JVM verifies the bytecode.
5. Execu on: Execu on Engine interprets or compiles bytecode.
6. Output/Result: The program runs and provides output.
7. Java Pla orm Components
Component Descrip on
JVM Runs Java bytecode; pla orm-specific implementa on.
JRE JVM + standard class libraries (used to run Java apps).
JRE + development tools (compiler, debugger, etc.) — used for developing Java
JDK
applica ons.
8. JDK vs JRE vs JVM
Feature JVM JRE JDK
Executes Java bytecode
Contains JVM
Contains Libraries
Contains Development Tools
Used to Run Apps
Used to Develop Apps
9. Summary & Key Takeaways
Java is a powerful, secure, object-oriented, and pla orm-independent language.
The JVM allows Java programs to run on any pla orm without modifica on.
Java’s structure (JDK → JRE → JVM) provides a comprehensive development-to-run me
environment.
JVM’s internal design ensures efficient memory management, robust execu on, and
security.
Understanding the JVM is cri cal for Java developers, especially for performance tuning
and debugging.