Friday, April 29, 2011

What is classloader?

The Class loader concept, one of the cornerstones of the Java virtual machine, describes the behavior of converting a named class into the bits responsible for implementing that class.
Source code to Machine code
In contrary to such languages like C++ or Fortran where source code is compiled directly to machine’s native code, Java’s source code is compiled to platform-independent Java bytecode. Normally Java Virtual Machine loads this bytecode for every required class from .class files from the local file system. Java gives us however a possibility to greatly influence the way bytecode is load – customized class loaders.

The loading of Java classes is performed by class loaders (CL), they are responsible for loading classes into the JVM. Simple applications can use the Java platform’s built-in class loading facility to load their classes, more complex applications tend to define their own custom class loaders. Because of this, the Java run time does not need to know anything about files and file systems when running Java programs.

Some points to note about class loaders
  • All class loaders are of the type java.lang.ClassLoader.
  • There are 2 types of classloaders mainly - Bootstrap loader or Primordial classloader and "other" class loaders which include extension class loaders and system class loaders
  • Root loader is bootstrap class loader which has native implementation and cannot be instantiated by Java code.
  • Other than the bootstrap class loader all class loaders have a parent class loader.
  • Chaining” of class loaders means, that every class loader has a parent class loader, and in most cases it asks its parent to load a requested class before trying to resolve it on its own. If the parent can not find the class, the class loader itself tries to load the class.

No comments:

Post a Comment