This article explains all you need to know to understand how dynamic linking in Java works.
Dynamic linking in Java
Image may be NSFW.
Clik here to view.Java applications are typically compiled to bytecode that can run on any Java Virtual Machine (JVM) regardless of computer architecture. The bytecode is an intermediate representation, which is then, linked to platform-specific machine code.
To understand the dynamic linking we must first divide Java program execution into steps. Java language specification defines 5 of them.
Evaluation
This is the first step which covers all the tasks before the linking process begins. These can be for example assignments or conditionals.
Loading
In this process, all the binary representations for class or interface names are loaded. These are loaded by class loaders when they are required for execution or for establishing a relationship as a subtype. Each class is loaded once. All the parent classes as well as interfaces of a given class are loaded. In case of an interface, all the superinterfaces are loaded. Loading process may fail when a class cannot be found, is badly formed or has a circularity.
Possible errors: NoClassDefFoundError
, ClassCircularityError
, ClassFormatError
Verification
In this step, each loaded class representation is checked whether it is well formed. If this check fails, a verification exception is thrown. Things checked during the verification process are:
- structural correctness of the bytecode,
- destinations of the goto instructions,
- possible stack overflow or underflow issues,
- types,
- hierarchy of inheritance,
- correctness of fields and method receivers and arguments
Preparation
In this process, a method lookup table is created. This means that once the table is created there is no need to check the superclasses for that. All the class variables as well as constants are created and initialized with their default values. This tasks can fail with OutOfMemory exception.
Resolution
In this step, the binary files are checked in terms of symbolic reference correctness. This means that all the referenced classes, interfaces, fields and methods are checked if they exists in a referenced class or interface. If they are correct, the symbolic link is changed to a direct link. If the resolution process fails, an IncompatibleClassChangeError (or its subclass) will occur.
Summary
All last four processes – loading, verification, preparation and resolution are parts of dynamic linking on Android. During the dynamic linking process the linker loads and links the libraries required by the process that is about to run.
The post Dynamic linking in Java. appeared first on stunning co.de.