-
Notifications
You must be signed in to change notification settings - Fork 7
Java Basics(JVM)
Every developer who uses Java knows that Java bytecode runs in a JRE (Java Runtime Environment). The most important element of the JRE is Java Virtual Machine (JVM), which analyzes and executes Java byte code.
The JRE is composed of the Java API and the JVM. The role of the JVM is to read the Java application through the Class Loader and execute it along with the Java API.
Java bytecode: To implement WORA(Write Once Run Anywhere), the JVM uses Java bytecode, a middle-language between Java (user language) and the machine language. This Java bytecode is the smallest unit that deploys the Java code. Java Bytecode is the essential element of JVM. The JVM is an emulator that emulates the Java Bytecode. Java compiler does not directly convert high-level language such as C/C++ to the machine language (direct CPU instruction); it converts the Java language that the developer understands to the Java Bytecode that the JVM understands. Since Java bytecode has no platform-dependent code, it is executable on the hardware where the JVM (accurately, the JRE of the same profile) has been installed, even when the CPU or OS is different (a class file developed and compiled on the Windows PC can be executed on the Linux machine without additional change.). The class file itself is a binary file that cannot be understood by a human. To manage this file, JVM vendors provide javap, the disassembler. The result of using javap is called Java assembly.
A class loader loads the compiled Java Bytecode to the Runtime Data Areas, and the execution engine executes the Java Bytecode.
Java provides a dynamic load feature; it loads and links the class when it refers to a class for the first time at runtime, not compile time. JVM's class loader executes the dynamic load. The features of Java class loader are as follows:
-
Hierarchical Structure: Class loaders in Java are organized into a hierarchy with a parent-child relationship. The Bootstrap Class Loader is the parent of all class loaders.
-
Delegation mode: Based on the hierarchical structure, load is delegated between class loaders. When a class is loaded, the parent class loader is checked to determine whether or not the class is in the parent class loader. If the upper class loader has the class, the class is used. If not, the class loader requested for loading loads the class.
-
Visibility limit: A child class loader can find the class in the parent class loader; however, a parent class loader cannot find the class in the child class loader.
-
Unload is not allowed: A class loader can load a class but cannot unload it. Instead of unloading, the current class loader can be deleted, and a new class loader can be created.
Each class loader has its namespace that stores the loaded classes. When a class loader loads a class, it searches the class based on FQCN (Fully Qualified Class Name) stored in the namespace to check whether or not the class has been already loaded. Even if the class has an identical FQCN but a different namespace, it is regarded as a different class. A different namespace means that the class has been loaded by another class loader.

-
Bootstrap class loader: This is created when running the JVM. It loads Java APIs, including object classes. Unlike other class loaders, it is implemented in native code instead of Java.
-
Extension class loader: It loads the extension classes excluding the basic Java APIs. It also loads various security extension functions.
-
System class loader: If the bootstrap class loader and the extension class loader load the JVM components, the system class loader loads the application classes. It loads the class in the $CLASSPATH specified by the user.
-
User-defined class loader: This is a class loader that an application user directly creates on the code.
Java platform has two components JVM and Java API
JVM ( Java Virtual Machine ): It is a software that cam be ported onto various hardware platforms. And converts Byte codes into machine level language.
-
Java Source Code(.java file) --> Java Compiler(javac/JIT) --> ByteCode(.class file).
-
A JVM converts bytecode into machine level language.
-
A jvm is a runtime environment used to run bytecode generated form the source code.
-
The class file is loaded,converted into machine language and run in the JVM. Each platform has its own kind of JVM.
-
Class loader : Loads the class files
-
Class Method Area : All the methods and method data are stored.
-
HEAP: All the OBJECTS are stored here.
-
STACK: Java stack stores frames, it holds local variables and partial results and plays a part in method invocation and return values.
-
PC (Program Counter )Register: Contains address of JVM instructions currently being executed.
-
Native Method Stack : All native methods used in application.
-
Execution Engine :
- A Virtual processor
- Interpreter ( Reads bytecode streams then executed instruction )
- JIT Complier (Just In Time)
JIT Compiler : A JIT (Just In Time) Compiler is used to improve the performance. A JIT – Compiler compiles parts of byte code that have similar functionality at the same time, and hence reduces the time needed.
Java API: Application Program Interface.
Garbage Collection
Class Loaders
- Boot Strap or System Class Loader:Boot strap is responsible to load the java API classes written by the sun developers. It is responsible to load Jdk’s internal classes.
- User defined Class Loader:
Heap and Stack
What happens at Object Creation Time
What happens atClass Loading Time

