How Java Works
How Java Works
import java.lang.reflect.Field;
import java.lang.reflect.Method;
System.out.println(c1.getName());
System.out.println(method.getName());
System.out.println(field.getName());
class Student {
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.
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 करने की सुकवधा प्रदान करता है।