Core Java With OCJP/SCJP:JVM Architecture  Part- 1||Introduction || class loading of system

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 .class files 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 .class files 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 .class files 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 .class file 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 .class files (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 .class file loaded, which represents the information contained in that file.
  • After loading a .class file, 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 .class file, emphasizing that it is indeed a class object.

Memory Areas Involved

  • Three memory areas are involved during this process: the hard disk (where .class files 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 Student class, 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 Student class 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 Student class with methods like getName() and getMarks().
  • The discussion includes using reflection to count declared methods in the Student class, emphasizing the method retrieval process.

Method Count in Classes

  • The speaker compiles and runs the code, noting that there are two methods: getName() and getMarks(), confirming understanding of method counting.
  • A comparison is made with the String class from Java.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 .class file 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 Student objects, 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 == C2 returns 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() and C2.hashCode() return the same value, reinforcing that they reference the same object.
Video description

java Basic Java Tutorial for beginners Basic Java Programming for beginners Core Java By Durga Sir Core Java Core Java Video Tutorials Core Java Tutorial for beginners with examples Core Java Tutorial Core Java DURGASOFT Durgasoft Core Java Durgasoft Java durga software solutions core java videos durga software solutions java How to learn java How to learn java programming language for beginners learn java learn java programming for beginners programming in java understanding java java application development tutorial java beginner tutorial java basics for beginners java for beginners java lessons java lectures java language tutorial for beginners java programming tutorial java programming for beginners java tutorial for beginners java tutorial java tutorial by durga sir ==================================== Java tutorial by durga sir https://goo.gl/XWb4RL Java 9 by durga sir https://goo.gl/hXGyBW Java 1.8 Version New Features by Durga sir https://goo.gl/iHXXYU Adv Java JDBC Tutorial by Durga sir https://goo.gl/8q16Eo OCJA 1.8 Java SE 8 Programmer - I (1Z0 - 808 ) By Durga sir https://goo.gl/gC6R7f Core Java by NagoorBabu sir https://goo.gl/s6Nvj1 Advenced Java by Nagoorbabu sir https://goo.gl/ZZonzJ CoreJava by Ratan https://goo.gl/3VM19v Advanced Java jdbc by Ratan https://goo.gl/Rn2UXr Advjava tutorials - JSP by Ratan https://goo.gl/Z6ytxm Adv java servlets tutorial by ratan https://goo.gl/zTwi9y Servlet and JSP Tutorial by anji reddy https://goo.gl/jZMRUv Advanced Java Jdbc by Anjireddy https://goo.gl/16CGzX Hibernate byAnjireddy https://goo.gl/qQojvZ Struts by Anjireddy https://goo.gl/nE1Eof Spring by Mr.AnjiReddy https://goo.gl/NfN14R ADV JAVA by Naveen https://goo.gl/bhSsXF Spring by Mr.Naveen https://goo.gl/huVwFN Hibernate by Mr. Naveen https://goo.gl/TY3Wpd Struts by Mr.Naveen https://goo.gl/Vkmiw7