Core Java With OCJP/SCJP:JVM Architecture Part- 1||Introduction || class loading of system
Understanding the Java Virtual Machine Architecture
Overview of JVM Components
- The class loader subsystem is responsible for loading
.classfiles into memory, which is a crucial first step in executing Java programs.
- After loading, the execution engine takes over to run the loaded classes. It reads and executes the bytecode from these class files.
- The JVM consists of various memory areas where class files are loaded, including method area, heap area, stack area, PC registers, and native method stacks.
- The execution engine interacts with native method libraries through the Java Native Interface (JNI), which acts as a bridge between Java code and native applications or libraries.
- At a high level, the JVM architecture can be summarized as having three main components: class loader subsystem, various memory areas, and execution engine.
Detailed Functions of Class Loader Subsystem
- The primary functions of the class loader subsystem include loading
.classfiles into memory and preparing them for execution by linking them appropriately.
- The process involves three key activities: loading (reading class files), linking (resolving references), and initialization (setting up static variables).
- Each activity plays an essential role in ensuring that classes are correctly prepared for execution within the JVM environment.
Understanding the Class Loader Subsystem in JVM
Responsibilities of the Class Loader Subsystem
- The class loader subsystem is responsible for three main activities: loading, linking, and initialization of classes.
- Loading involves reading the compiled
.classfiles from storage (e.g., hard disk) into memory. This process begins after compilation generates these files.
The Loading Process
- During loading, the JVM reads the binary data from the
.classfile and stores it in a specific memory area known as the method area.
- Key information stored during loading includes:
- Fully qualified name of the class.
- Fully qualified name of its parent class.
- Method and variable information, including constructors and modifiers.
Metadata Storage in JVM
- For each loaded class file, JVM stores corresponding metadata in the method area. This includes:
- Complete details about methods and variables.
- Constant pool information which holds constants used within that class.
Object Creation Post Loading
- After loading, JVM creates an object in the heap area based on the loaded class file's information. This object is of type
Class, which represents metadata about that particular class.
- The example provided illustrates two
.classfiles (Student and Customer), both stored on a hard disk but loaded into different areas within JVM—specifically, their metadata goes to method area while objects are created in heap area.
Summary of Memory Areas
Understanding JVM Class Loading and Class Objects
JVM Class Object Creation
- The Java Virtual Machine (JVM) creates a class object for each
.classfile loaded, which represents the information contained in that file.
- After loading a
.classfile, the JVM generates a class object in the heap area to represent that specific class's information.
- It is important to distinguish between student objects and class objects; the created object is a class object, not an instance of the student or customer classes.
- The JVM ensures clarity regarding what type of object is being created after loading each
.classfile, emphasizing that it is indeed a class object.
Memory Areas Involved
- Three memory areas are involved during this process: the hard disk (where
.classfiles are stored), method area (for storing metadata about classes), and heap area (where instances of classes are created).
- The distinction between student/customer objects and their corresponding class objects must be clear for understanding how Java manages memory.
Accessing Class-Level Information
- Programmers can utilize class objects to retrieve information at the class level, such as methods, variables, and constructors associated with that particular class.
- For example, when querying how many methods exist within a
Studentclass, programmers can load this information using specific commands in Java.
Loading Classes Dynamically
- Using
Class.forName(), developers can dynamically load classes. This action triggers the creation of a corresponding class object by the JVM.
- Upon loading a
Studentclass through this method, both method area storage and heap allocation occur simultaneously for efficient access to metadata.
Retrieving Method Information
- Once you have access to the class object, you can retrieve all declared methods using reflection APIs like
getDeclaredMethods().
- Each retrieved method can then be printed out by iterating through them with system output commands.
Import Statements Requirement
- To work with reflection in Java effectively, necessary import statements must be included; specifically importing from
java.lang.reflect.
Counting Methods Programmatically
Understanding Java Reflection and Class Objects
Introduction to Java Reflection
- The speaker introduces a simple Java file example, focusing on the
Studentclass with methods likegetName()andgetMarks().
- The discussion includes using reflection to count declared methods in the
Studentclass, emphasizing the method retrieval process.
Method Count in Classes
- The speaker compiles and runs the code, noting that there are two methods:
getName()andgetMarks(), confirming understanding of method counting.
- A comparison is made with the
Stringclass fromJava.lang, revealing it has approximately 72 to 76 methods.
Exploring Object Class Methods
- Inquiry into how many methods exist in the Object class leads to a clarification that there are 12 methods, including one often overlooked (
registerNatives()).
- Most programmers typically consider only 11 of these methods due to common usage patterns.
Role of Class Objects in JVM
- The speaker explains that the Class object is used internally by the JVM but can also be utilized by programmers for obtaining class-level information.
Loading Classes and Creating Objects
- An important point is made about creating an instance of a class (e.g.,
Student) which triggers loading its corresponding.classfile into memory.
- Upon instantiation, a Class object representing this loaded class is created within JVM's heap area.
Reusability of Class Objects
- When multiple instances of a class are created, such as multiple
Studentobjects, the corresponding Class object is not recreated; it exists only once.
- This means that even if you create several instances (like S1 and S2), they reference the same underlying Class object.
Understanding Class Objects in Java
The Concept of Class Objects
- C1 and C2 are references pointing to the same object, demonstrating that multiple references can exist for a single class instance.
- Since both C1 and C2 reference the same object, their hash codes will be identical, confirming they point to the same memory location.
- The equality check
C1 == C2returns true because both variables refer to the same object instance.
Code Execution and Results
- After executing the code, it is confirmed that both
C1.hashCode()andC2.hashCode()return the same value, reinforcing that they reference the same object.