0% found this document useful (0 votes)
54 views

How Java Works

The document discusses how a Java program works internally. It explains that when a Java program is compiled, it is not converted directly to machine language but rather an intermediate language called Java bytecodes. These bytecodes are platform independent and can run on any operating system or processor. When a Java program is run, the Java Virtual Machine (JVM) interprets the bytecodes rather than compiling them to machine code directly. The JVM acts like an abstraction layer between the bytecodes and actual hardware, allowing Java programs to run on any system with a compatible JVM.

Uploaded by

Mohit Gautam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

How Java Works

The document discusses how a Java program works internally. It explains that when a Java program is compiled, it is not converted directly to machine language but rather an intermediate language called Java bytecodes. These bytecodes are platform independent and can run on any operating system or processor. When a Java program is run, the Java Virtual Machine (JVM) interprets the bytecodes rather than compiling them to machine code directly. The JVM acts like an abstraction layer between the bytecodes and actual hardware, allowing Java programs to run on any system with a compatible JVM.

Uploaded by

Mohit Gautam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

How JVM Works – JVM Architecture?

JVM(Java Virtual Machine) acts as a run-time engine to run Java applications.


JVM is the one that actually calls the main method present in a java code.
JVM is a part of JRE(Java Runtime Environment).
Java applications are called WORA (Write Once Run Anywhere). This means
a programmer can develop Java code on one system and can expect it to run
on any other Java-enabled system without any adjustment. This is all possible
because of JVM.
When we compile a .java file, .class files(contains byte-code) with the same
class names present in .java file are generated by the Java compiler.
This .class file goes into various steps when we run it. These steps together
describe the whole JVM.

Class Loader Subsystem

It is mainly responsible for three activities.


• Loading
• Linking
• Initialization
Loading: The Class loader reads the “.class” file, generate the corresponding
binary data and save it in the method area. For each “.class” file, JVM stores
the following information in the method area.
• The fully qualified name of the loaded class and its immediate parent
class.
• Whether the “.class” file is related to Class or Interface or Enum.
• Modifier, Variables and Method information etc.
After loading the “.class” file, JVM creates an object of type Class to represent
this file in the heap memory. Please note that this object is of type Class
predefined in java.lang package. These Class object can be used by the
programmer for getting class level information like the name of the class,
parent name, methods and variable information etc. To get this object
reference we can use getClass() method of Object class.
• Java

// A Java program to demonstrate working

// of a Class type object created by JVM

// to represent .class file in memory.

import java.lang.reflect.Field;

import java.lang.reflect.Method;

// Java code to demonstrate use

// of Class object created by JVM

public class Test {

public static void main(String[] args)

Student s1 = new Student();

// Getting hold of Class

// object created by JVM.


Class c1 = s1.getClass();

// Printing type of object using c1.

System.out.println(c1.getName());

// getting all methods in an array

Method m[] = c1.getDeclaredMethods();

for (Method method : m)

System.out.println(method.getName());

// getting all fields in an array

Field f[] = c1.getDeclaredFields();

for (Field field : f)

System.out.println(field.getName());

// A sample class whose information

// is fetched above using its Class object.

class Student {

private String name;

private int roll_No;


public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getRoll_no() { return roll_No; }

public void setRoll_no(int roll_no)

this.roll_No = roll_no;

Output
Student
getName
setName
getRoll_no
setRoll_no
name
roll_No
Note: For every loaded “.class” file, only one object of the class is created.

Student s2 = new Student();


// c2 will point to same object where
// c1 is pointing
Class c2 = s2.getClass();
System.out.println(c1==c2); // true
Linking: Performs verification, preparation, and (optionally) resolution.

• Verification: It ensures the correctness of the .class file i.e. it checks


whether this file is properly formatted and generated by a valid
compiler or not. If verification fails, we get run-time
exception java.lang.VerifyError. This activity is done by the
component ByteCodeVerifier. Once this activity is completed then
the class file is ready for compilation.
• Preparation: JVM allocates memory for class variables and
initializing the memory to default values.
• Resolution: It is the process of replacing symbolic references from
the type with direct references. It is done by searching into the
method area to locate the referenced entity.
Initialization: In this phase, all static variables are assigned with their values
defined in the code and static block(if any). This is executed from top to bottom
in a class and from parent to child in the class hierarchy.
In general, there are three class loaders :

• Bootstrap class loader: Every JVM implementation must have a


bootstrap class loader, capable of loading trusted classes. It loads
core java API classes present in the “JAVA_HOME/jre/lib” directory.
This path is popularly known as the bootstrap path. It is implemented
in native languages like C, C++.
• Extension class loader: It is a child of the bootstrap class loader. It
loads the classes present in the extensions directories
“JAVA_HOME/jre/lib/ext”(Extension path) or any other directory
specified by the java.ext.dirs system property. It is implemented in
java by the sun.misc.Launcher$ExtClassLoader class.
• System/Application class loader: It is a child of the extension class
loader. It is responsible to load classes from the application
classpath. It internally uses Environment Variable which mapped to
java.class.path. It is also implemented in Java by
the sun.misc.Launcher$AppClassLoader class.
• Java

// Java code to demonstrate Class Loader subsystem

public class Test {

public static void main(String[] args)

// String class is loaded by bootstrap loader, and

// bootstrap loader is not Java object, hence null

System.out.println(String.class.getClassLoader());
// Test class is loaded by Application loader

System.out.println(Test.class.getClassLoader());

Output

null
jdk.internal.loader.ClassLoaders$AppClassLoader@8bcc55f
Note: JVM follows the Delegation-Hierarchy principle to load classes. System
class loader delegate load request to extension class loader and extension
class loader delegate request to the bootstrap class loader. If a class found in
the boot-strap path, the class is loaded otherwise request again transfers to
the extension class loader and then to the system class loader. At last, if the
system class loader fails to load class, then we get run-time
exception java.lang.ClassNotFoundException.

JVM Memory
1. Method area: In the method area, all class level information like
class name, immediate parent class name, methods and variables
information etc. are stored, including static variables. There is only
one method area per JVM, and it is a shared resource.
2. Heap area: Information of all objects is stored in the heap area.
There is also one Heap Area per JVM. It is also a shared resource.
3. Stack area: For every thread, JVM creates one run-time stack which
is stored here. Every block of this stack is called activation
record/stack frame which stores methods calls. All local variables of
that method are stored in their corresponding frame. After a thread
terminates, its run-time stack will be destroyed by JVM. It is not a
shared resource.
4. PC Registers: Store address of current execution instruction of a
thread. Obviously, each thread has separate PC Registers.
5. Native method stacks: For every thread, a separate native stack is
created. It stores native method information.

Execution Engine
Execution engine executes the “.class” (bytecode). It reads the byte-code line
by line, uses data and information present in various memory area and
executes instructions. It can be classified into three parts:
• Interpreter: It interprets the bytecode line by line and then executes.
The disadvantage here is that when one method is called multiple
times, every time interpretation is required.
• Just-In-Time Compiler(JIT) : It is used to increase the efficiency of an
interpreter. It compiles the entire bytecode and changes it to native
code so whenever the interpreter sees repeated method calls, JIT
provides direct native code for that part so re-interpretation is not
required, thus efficiency is improved.
• Garbage Collector: It destroys un-referenced objects. For more on
Garbage Collector, refer Garbage Collector.
Java Native Interface (JNI) :
It is an interface that interacts with the Native Method Libraries and provides
the native libraries(C, C++) required for the execution. It enables JVM to call
C/C++ libraries and to be called by C/C++ libraries which may be specific to
hardware.
How Java Program Works Internally?
How Java Program Works Internally: जब Java के ककसी Program को Compile ककया जाता है तब Java
का Program पूरी तरह से Machine Language में Convert नहीीं होता है बल्कि एक Intermediate Language
में Convert होता है , कजसे Java Bytecodes कहा जाता है । ये Codes Platform Independent होते हैं , इसकिए
इन्हें ककसी भी Operating System व ककसी भी Processor पर चिाया जा सकता है। Java के Program
की Compilation केवि एक ही बार होती है िेककन कजतनी बार भी Java के Program को चिाया जाता है , हर
बार उस Program का Interpretation होता है । इसे हम कनम्न कचत्र द्वारा समझ सकते हैं -

Java Bytecodes को हम Java Virtual Machine (Java VM) के किए Machine Codes मान सकते हैं । हर
Java Interpreter, चाहे वह Java Development Tool हो या कोई Browser जो कक Java Applets को Run
करता हो, Java Virtual Machine का ही Implementation है । Java Virtual Machine को Hardware में भी
Implement ककया जा सकता है , कजसका पररणाम आज के Mobile System Software हैं।
Java Bytecodes हमें ये सुकवधा दे ते हैं कक हम Java के Program को एक बार Compile करें और कहीीं भी Run
करें । हम ककसी Java Program को ककसी भी उस Computer पर Compile कर सकते हैं कजस पर Java Compiler
हो। किर उस Java Program के Bytecodes को ककसी भी उस Computer पर Run ककया जा सकता है कजस
पर Java VM हो। उदाहरण के किए एक ही Java Program Windows, OS/2 MacOS NT, Macintosh आकद
कवकभन्न Platform पर Execute हो सकते हैं ।

Java Platform
Platform एक एक ऐसा Software या Hardware Environment होता है कजसमें कोई Program Run होता है ।
Java Platform कई अन्य Platforms से अिग है । Java Platform एक Software Platform है , जो सभी अन्य
Hardware Based Platform के Top पर यानी ऊपर Run होता है । ज्यादातर अन्य Platforms Hardware व
Operating System का Combination होते हैं। Java Platform के दो Components हैं।
Read also: Differences between a Java Applet and an Application

▪ पहिा है Java Virtual Machine (Java VM) कजसके बारे में हम जान चुके हैं । ये Java Platform
का Base या आधार है और कवकभन्न Hardware Base Platform के ऊपर रहता है ।
▪ दू सरा होता है Java Application Programming Interface (Java API) कजसके बारे में हम
अब जानेंगे।
Java API, Ready Made Software Components का एक बहुत बडा Collection है जो कक Programmer को
GUI (Graphical User Interface) जैसी कई उपयोगी क्षमताएीं प्रदान करता है । Java API को Related
Components की Libraries के रूप में Group कर कदया गया है । इन कवकभन्न Related Components के Group
को ही Packages कहते हैं । जो कक कुछ हद तक C/C++ की Header Files के समान ही होता है , िेककन C की
Header File एक प्रकार से Functions का Collection होती है , जबकक Java का Package, एक प्रकार से
Classes का Collection होता है । एक Java Program को हम कनम्न कचत्रानुसार दर्ाा सकते हैं -

जब एक Java Program को ककसी Computer पर Execute ककया जाता है तो Java Program व Hardware
Based Platform के बीच Java API व Java Virtual Machine की Layer होती हैं जो Java के Program को
Hardware Dependencies से अिग करती हैं । यानी इन दोनोीं की वजह से Java का कोई Program ककसी भी
Computer के Hardware पर कनभार नहीीं होता है । एक Platform Independent Environment के रूप में Java
का Program अन्य Native Codes Programs की तुिना में कुछ धीमा होता है । िेककन किर भी अच्छे Compilers,
Java के साथ अच्छी तरह से Tune होने वािे Interpreters और Bytecodes Compilers की वजह से Java की
Performance को Native Code की Performance के आस पास िाया जा सकता है और वो भी जावा की सभी
कवर्ेषताओीं के साथ।
Java Programs का सबसे अकधक जाना पहचाना यकद कोई रूप है तो वह Java Applets का है । एक Applet भी
एक Java Program ही होता है िेककन इसकी कवर्ेषता ये है कक ये ककसी Java Enabled Browser जैसे
कक Internet Explorer, Netscape Navigator, Google Chrome, Opera, FireFox आकद में Run होता है ,
स्वतींत्र रूप से ये Run नहीीं हो सकता। जबकक Java Application Standalone Run हो सकते हैं । Applets
Application के समान ही होते हैं ।
ऐसा भी नहीीं है कक Java का प्रयोग केवि Web Pages Applications किखने के किए ही ककया जा सकता है । बल्कि
Java एक Powerful Software Platform और General Purpose, High Level Programming
Language भी है। Java के सबसे Common Application Programs के उदाहरण Servers हैं जो
ककसी Network के कवकभन्न Clients को Service प्रदान करने का काम करते हैं। Web Servers, Proxy Servers,
Mail Servers, Print Servers व Boot Servers Java Applications के कवकभन्न उदाहरण हैं । Servlets Applets
के समान ही होते हैं िेककन ये ककसी Browser में Run होने के बजाय ये Java Servers में Run होते हैं और Java
Server की Configuring या Tailoring करते हैं ।
Read also: How to create a Simple Applet Program in Java

एक सवाि पैदा हो सकता है कक Java API इन सभी प्रकार के Programs को ककस प्रकार से Support करता है ।
इसका जवाब ये है कक ये इन सभी प्रकार के Programs को एक Software Components के Package के माध्यम
से Support करता है कजसमें कवकभन्न प्रकार की Functionalities होती हैं । Core API एक ऐसा API है जो हर Java
Platform में पूरी तरह से Implemented होता है। Core API हमें कनम्न Features प्रदान करता है -
The Essentials:
Objects, strings, threads, numbers, input and output, data structures, system properties, date and
time जैसी कई चीजोीं को Handle करने की सुकवधा प्रदान करता है ।
Applets:
Java applets बनाने के किए कवकभन्न Components प्रदान करता है ।
Networking:
Networking की सुकवधा प्राप्त करने के किए URLs, TCP व UDP sockets तथा IP addresses प्रदान करता है ।
Internationalization:
ये हमें ऐसी सुकवधा प्रदान करता है कक हम ऐसे Programs किख सकते हैं जो सारी दु कनया में समान रूप से चि सकता
है ।
Read also: Object Oriented Programming Concepts in Java

Security:
ये हमें Low-level और high-level दोनोीं प्रकार की सुरक्षा प्रदान करता है । साथ ही electronic signatures,
public/private key management, access control और certificates की भी सुकवधा प्रदान करता है ।
Software components:
ये हमें JavaBeans जैसे Components प्रदान करता है जो ककसी पहिे से बने हुए Component Architecture में
जैसे कक Microsoft’s OLE/COM/Active-X architecture, OpenDoc और Netscape’s Live Connect में Plug
in हो सकता है ।
Object serialization:
ये हमें Remote Method Invocation (RMI) द्वारा दू सरे सरि उपकरणोीं से Communication करने की सुकवधा
प्रदान करता है , कजसका प्रयोग आज Mobile Technology में भी हो रहा है ।
Java Database Connectivity (JDBC):
ये हमें Relational databases से Connect होने व उन्हें Access करने की सुकवधा प्रदान करता है।

You might also like