0% found this document useful (0 votes)
11 views1 page

Java JVM Architecture and Concepts

The document provides an overview of the Java Virtual Machine (JVM) architecture, including its components like Class Loader, Method Area, and Execution Engine, as well as garbage collection and memory management. It also compares abstract classes and interfaces, highlighting their differences and use cases, and discusses exception handling in Java. Additionally, it outlines the applet lifecycle, detailing its methods and flow.

Uploaded by

spandey67195
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Java JVM Architecture and Concepts

The document provides an overview of the Java Virtual Machine (JVM) architecture, including its components like Class Loader, Method Area, and Execution Engine, as well as garbage collection and memory management. It also compares abstract classes and interfaces, highlighting their differences and use cases, and discusses exception handling in Java. Additionally, it outlines the applet lifecycle, detailing its methods and flow.

Uploaded by

spandey67195
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

JVM Architecture Introduction Introduction


The Java Virtual Machine (JVM) is a runtime environment that executes Java bytecode. It provides platform In Java, both abstract classes and interfaces are used to achieve abstraction. They provide a way to define
The JVM executes Java bytecode and provides platform independence.
independence, memory management, and security. JVM converts compiled .class files into machine code using methods that must be implemented by child classes. But there are key differences in their features and usage.
Components:
its internal architecture.
- Class Loader: Loads .class files
Key Differences (Table Form – exam me likhne ka best tarika)
- Method Area: Stores class structures
Diagram Feature Abstract Class Interface
- Heap: Objects
Class Loader Keyword abstract interface
- Stack: Method calls/local variables
Methods Can have abstract + concrete methods Only abstract methods (till Java 7)
- PC Register: Current instruction
------------------- Variables Can have instance & static variables Only public static final (constants)
- Execution Engine: Interpreter + JIT
| Method Area | Inheritance Supports single inheritance Supports multiple inheritance
- JNI & Native Libraries: For non-Java code
| Heap | Constructors Can have constructors Cannot have constructors
Garbage Collector automatically frees unused memory.
| Java Stack | Access Modifiers Methods can be public/protected All methods are public abstract by default
Flow: Class Loader Bytecode Verifier Execution Engine Output.
| PC Register | Implementation Class extends abstract class Class implements interface
2. Garbage Collection | Native Stack |
Automatic memory management in Java. ------------------- Code
Unused objects (no reference) are deleted by Garbage Collector. // Abstract Class abstract class Animal { abstract void sound(); void sleep() { [Link]("Sleeping..."); } } //
Request GC: Execution Engine Interface interface Pet { void play(); } // Implementation class Dog extends Animal implements Pet { void sound() {
- [Link](); (Interpreter + JIT) [Link]("Bark"); } public void play() { [Link]("Playing..."); } } class Test { public static void
- [Link]().gc(); main(String[] args) { Dog d = new Dog(); [Link](); [Link](); [Link](); } }
Advantages: Native Method Interface Native Libraries
- Prevents memory leaks Conclusion
- Improves performance Explanation of Components Abstract Class is used when classes share common behavior along with some abstract methods.
- Simplifies memory management 1. Class Loader – Loads .class files into JVM. Interface is used to achieve multiple inheritance and define contract-like behavior.
3. Abstract Class vs Interface 2. Method Area – Stores class info, methods, static variables. In modern Java (8+), interfaces can also have default and static methods, making them more powerful.
3. Heap – Stores objects and instance variables.
Abstract Class:
- Keyword: abstract 4. Java Stack – Holds method calls and local variables.
5. PC Register – Keeps track of current instruction.
- Can have abstract + concrete methods
6. Native Method Stack – Supports non-Java (C/C++) methods.
- Supports single inheritance- Can have constructors & variables Introduction
Interface: 7. Execution Engine – Runs bytecode.
Exception = runtime error that disrupts normal program flow.
- Keyword: interface Interpreter
Exception Handling in Java ensures program doesn’t crash abruptly, instead it handles errors gracefully.
- Only abstract methods (till Java 7) JIT Compiler
8. Native Method Interface (JNI) – Connects JVM with native libraries. It uses five keywords: try, catch, finally, throw, throws.
- Supports multiple inheritance
- Only public static final variables 9. Native Libraries – Precompiled libraries in C/C++ used by JVM.
Flow of Exception Handling
Use Abstract class for shared behavior; Interface for contract & multiple inheritance.
Conclusion 1. Code inside try block may generate exception.
4. Exception Handling The JVM provides a layer between Java programs and the operating system. With features like garbage 2. catch block.
Mechanism to handle runtime errors. collection, memory management, and security, it makes Java programs portable, secure, and efficient. 3. finally block always executes (whether exception occurs or not).
Keywords: 4. throw
- try: risky code 5. throws
- catch: handles exception Introduction
- finally: always executes In Java, memory management is automatic. The Garbage Collector (GC) is responsible for reclaiming memory Diagram
- throw: used to throw exception- throws: declares exceptions occupied by unused (unreachable) objects. Unlike C/C++, Java programmers don’t need to explicitly free memory. try catch finally
Advantages: | |
- Prevents abnormal termination How Garbage Collection Works handles error
- Provides robust programs 1. Object Creation Heap area. |
- Separates error-handling code from logic 2. Reachability Check garbage. if no error, skip catch, go to finally
5. Applet Lifecycle 3. Garbage Collector
Applet is Java program that runs inside browser/applet viewer. 4. Methods to Request GC: Program
Lifecycle Methods: [Link](); class Test { public static void main(String[] args) { try { int a = 10 / 0; // ArithmeticException } catch
(ArithmeticException e) { [Link]("Error: " + e); } finally { [Link]("Program Ended Safely!"); } } }
- init(): initialize applet [Link]().gc();
Output:
- start(): starts execution
Error: [Link]: / by zero
- paint(): displays output Diagram
Program Ended Safely!
- stop(): suspends execution- destroy(): cleanup Heap Memory
Flow: init() start() paint() stop() destroy() -----------------------
User-Defined Exception
class InvalidAgeException extends Exception { InvalidAgeException(String s){ super(s); } } class Test { public static
void main(String args[]) throws InvalidAgeException { int age = 15; if(age < 18) throw new InvalidAgeException("Not
-----------------------
eligible to vote"); } }

Program
Advantages
class Test { public static void main(String[] args) { Test t1 = new Test(); Test t2 = new Test(); t1 = null; // eligible for GC
Prevents abnormal termination.
t2 = null; // eligible for GC [Link](); // request garbage collection } protected void finalize() {
[Link]("Object destroyed by GC"); } } Separates error handling code from normal code.
Provides robust & secure applications.
Advantages of Garbage Collection
Prevents memory leaks. Conclusion
Java’s Exception Handling makes programs error-resilient by providing a structured way to detect, handle, and
Improves performance of applications.
recover from runtime errors.
Simplifies memory management for programmers.

Conclusion
Garbage Collection in Java ensures automatic memory management, making Java more secure, robust, and Introduction
reliable compared to languages where manual memory management is required. An applet is a Java program that runs inside a web browser or an applet viewer.
Unlike normal Java applications (with main()), applets follow a special lifecycle controlled by
the browser/JVM.

Applet Lifecycle Methods


1. init()
Called once when the applet is created.
Used to initialize variables, UI, etc.
2. start()
Called after init().
Executes each time the applet becomes active (e.g., browser reload).
3. paint(Graphics g)
Called automatically when applet needs to display output.
Used for drawing shapes, text, images.
4. stop()
Called when the user navigates away from the applet page.
Suspends activities.
5. destroy()
Called just before the applet is removed from memory.
Used for cleanup (close connections, free resources).

Applet Lifecycle Flow


init start paint()

stop()

destroy()

Program
import [Link]; import [Link]; public class MyApplet extends Applet { public void init() {
[Link]("Applet initialized"); } public void start() { [Link]("Applet started"); } public void
paint(Graphics g) { [Link]("Hello Applet!", 50, 50); } public void stop() { [Link]("Applet stopped"); }
public void destroy() { [Link]("Applet destroyed"); } }

Conclusion
The Applet Lifecycle defines how an applet is created, displayed, paused, and destroyed.
Lifecycle methods (init(), start(), paint(), stop(), destroy()) provide a structured way to control the behavior of
applets.

You might also like