《java-面试题(英文版)》由会员分享,可在线阅读,更多相关《java-面试题(英文版)(574页珍藏版)》请在金锄头文库上搜索。
1、1 Learn Java/J2EE core concepts and key areas With Java/J2EE Job Interview CompanionByK.Arulkumaranimport java.io.File;import .URL;Q 05: Explain Java class loaders? If you have a class in a package, what do you need to do to run it? Explain dynamicclass loading? LF A 05: Class loaders are hierarchic
2、al. Classes are introduced into the JVM as they are referenced by name in a class thatis already running in the JVM. So, how is the very first class loaded? The very first class is especially loaded withthe help of static main( ) method declared in your class. All the subsequently loaded classes are
3、 loaded by theclasses, which are already loaded and running. A class loader creates a namespace. All JVMs include at least oneclass loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now lets look atnon-primordial class loaders. The JVM has hooks in it to allow
4、 user defined class loaders to be used in place ofprimordial class loader. Let us look at the class loaders created by the JVM.CLASS LOADER reloadable? ExplanationBootstrap(primordial)No Loads JDK internal classes, java.* packages. (as defined in the sun.boot.class.pathsystem property, typically loa
5、ds rt.jar and i18n.jar)Extensions No Loads jar files from JDK extensions directory (as defined in the java.ext.dirs systemproperty usually lib/ext directory of the JRE)System No Loads classes from system classpath (as defined by the java.class.path property, whichis set by the CLASSPATH environment
6、variable or classpath or cp command lineoptions)Bootstrap(primordial)(rt.jar, i18.jar)Extensions(lib/ext)System(-classpath)Sibling1classloaderSibling2classloaderJVM class loadersClasses loaded by Bootstrap class loader have no visibility into classesloaded by its descendants (ie Extensions and Syste
7、ms class loaders).The classes loaded by system class loader have visibility into classes loadedby its parents (ie Extensions and Bootstrap class loaders).If there were any sibling class loaders they cannot see classes loaded byeach other. They can only see the classes loaded by their parent classloa
8、der. For example Sibling1 class loader cannot see classes loaded bySibling2 class loaderBoth Sibling1 and Sibling2 class loaders have visibilty into classes loadedby their parent class loaders (eg: System, Extensions, and Bootstrap)Class loaders are hierarchical and use a delegation model when loadi
9、ng a class. Class loaders request theirparent to load the class first before attempting to load it themselves. When a class loader loads a class, the childclass loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loadedJava - Fundamentals 16by a child
10、class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not trueas explained in the above diagram.Q. What do you need to do to run a class with a main() method in a package?Example: Say, you have a class named “Pet” in a project folder “c:myProject” and pa
11、ckage namedcom.xyz.client, will you be able to compile and run it as it is?package com.xyz.client;public class Pet public static void main(String args) System.out.println(“I am found in the classpath“);To run c:myProject java com.xyz.client.PetThe answer is no and you will get the following exceptio
12、n: “Exception in thread “main“ java.lang.-NoClassDefFoundError: com/xyz/client/Pet”. You need to set the classpath. How can you do that? One of thefollowing ways:1. Set the operating system CLASSPATH environment variable to have the project folder “c:myProject”. Shownin the above diagram as the Syst
13、em classpath class loader2. Set the operating system CLASSPATH environment variable to have a jar file “c:/myProject/client.jar”, whichhas the Pet.class file in it. Shown in the above diagram as the System classpath class loader.3. Run it with cp or classpath option as shown below:c:java cp c:/myPro
14、ject com.xyz.client.PetORc:java -classpath c:/myProject/client.jar com.xyz.client.PetImportant: Two objects loaded by different class loaders are never equal even if they carry the same values, which mean aclass is uniquely identified in the context of the associated class loader. This applies to si
15、ngletons too, where each classloader will have its own singleton. Refer Q51 in Java section for singleton design patternQ. Explain static vs. dynamic class loading?Static class loading Dynamic class loadingClasses are statically loaded with Javas“new” operator.class MyClass public static void main(S
16、tring args) Car c = new Car();Dynamic loading is a technique for programmatically invoking the functions of aclass loader at run time. Let us look at how to load classes dynamically.Class.forName (String className); /static method which returns a ClassThe above static method returns the class object associated with the classname. The string className can be supplied dynamically at run time. Unlike thestatic loading, the dynamic loading will decide