Search code examples
javaclassloader

Java class loader efficiency


My question maybe trivial but after some research I am still not satisfied with the answers I got. My question is if using a third party class would be more efficient than using java core classes or java libraries.

as far as I know the order of class loaders in java to be invoked

  • System class loader
  • Extension class loader
  • Bootstrap class loader

wouldn't it be more efficient to use a third party jar ( which will be loaded by System class loader) instead of using built in class files, for example suppose I have a huge system which requires sorting many times, and lets suppose the algorithm I am planning to use is merge sort , so typically I would go on with using

                Collections.sort()

But wouldn't it be more efficient if I implement it myself and used it so I can get it from the first class loader?


Solution

  • The first class loader is the builtin class loader which is why it's not trivial to write your of Collections class.

    Classes are only loaded once so where they came from after loading doesn't matter to performance. Even on the first time, it is unlikely to matter much.

    BTW The System (your code) class loader comes last.

    Even if this were not the case, the builtin classes are often optimised in ways which are not apparent in the Java code and you couldn't write yourself. i.e. the JVM just replaces some methods with hand crafted native code. (Called intrinsics)