- Loading
-
Loading means reading the class file
for a type, parsing it to get its information, and storing the information in the
method area. Class loader sub-system follows delegation hierarchy
algorithm for this. Whenever JVM come across a particular class, first it will
check whether the corresponding class is already loaded or not. if it is loaded
in the method area then JVM will use that loaded class else JVM requests class
loader subsystem to load that particular class, then class loader subsystem
handover the request to application/system class loader. System class loader
delegates request to extension class loader and extension class loader in turn
delegates to boot strap class loader. Bootstrap class loader searches class in
bootstrap class path (
/jre/lib ). If the specified class is available then it will be loaded otherwise bootstrap class loader delegated the loading request to extension class loader. Extension class loader will search in extension class path (/jre/lib/ext ) or any other directory specified by the java.ext.dirs system property. If the specified class is available then it will be loaded otherwise request will be delegated to application/system class loader. Application class loader will search in application class path. If the specified class is available then it will be loaded(creation of an instance of java.lang.Class for the loaded type) else JVM will throw runtime exception : classNotFoundException
Note:
The bootstrap class
loader loads the core Java libraries located
in the /jre/lib directory. These libraries are stored
in Jar files called rt.jar, core.jar, server.jar,
etc. This class loader, which is part of the core JVM, is written in native
code. The
system class loader loads code found on java.class.path, which maps to
the CLASSPATH environment variable.
- Linking: The next process handled by the class loader is Linking. This involves three sub-processes: Verification, Preparation and Resolution
o
Verification - Verification is the process of ensuring that binary representation of a
class (bytecode) is structurally correct. The JVM has to make sure that a file
it is asked to load was generated by a valid compiler and it is well formed.
Some of the things that are checked at verification
are:
§ Every method is provided with a structurally correct
signature
§ Every instruction obeys the type discipline of the
Java language
§ Every branch instruction branches to the start not
middle of another instruction
Java compiler ensures that Java source code doesn't
violate the safety rules but, when an application imports a code fragment, it
doesn't actually know if the code fragment follows Java
language rules for safety. In other words, the code may not have been produced
by a trustworthy Java compiler. In that case, the Java run time system on your
machine has to assume the fragment is bad and subjects it to bytecode
verification.
The
Java virtual machine does not even see the bytecode until it's
been through this verification process. Doing this as the bytecode is loaded
also has the advantage that a whole lot of run time checks don't need to be
performed every time the code is executed. Because it's been verified as
correct, it can, once it starts running, run faster than would otherwise be
possible.
o
Preparation - In this phase, the JVM allocates memory for the class (i.e. static)
variables and sets them to default initial values. Note that class variables
are not initialized to their proper initial values until the initialization
phase - no java code is executed until initialization. The default values for
the various types are as follows :
byte(0), short(0), int(0), long(0L), float(0.0f), double(0.0d), Boolean(false)
o
Resolution - Resolution is the process of replacing symbolic names for types,
fields and methods used by a loaded type with their actual references. Symbolic
references are resolved into a direct reference by searching through the method
area to locate the referenced entity.
- Initialization: it is a process of setting class variables to their proper initial values - initial values desired by the programmer. Initialization of a class consists of two steps:
o
Initializing its direct superclass (if any
and if not already initialized)
o
Executing its own initialization statements
It means that, the first class that gets initialized
is Object (which is superclass of all the classes). However, static
final variables are not treated as class variables but as constants hence
their values are assigned at compilation.
Once class is loaded, linked,
and initialized, it is ready for use. Its static fields and static
methods can be used and it can be instantiated. When a new class instance
is created, memory is allocated for all its instance variables in the heap.
Memory is also allocated recursively for all the instance variables declared in
its superclass and all classes up is inheritance hierarchy. All instance
variables in the new object and those of its superclasses are then initialized
to their default values. The constructor invoked in the instantiation is
then processed. Finally, the reference to the newly created object is returned
as the result.
Related Topics :
No comments:
Post a Comment