Java EE interview Bible translation (1)

Source: Internet
Author: User
Tags garbage collection

Q1: Give some reasons to use Java.

A1:java is an interesting programming language, let me find out some reasons: built-in multithreaded mechanism, socket, memory management (Automatic garbage collection) object-oriented cross-platform support for web-based applications (applets, servlet, JSP) through the extension of the standard API Distributed Applications (sockets, RMI, EJBS) and network protocols (HTTP, JRMP, etc.).


What are the main differences between the Q2:java platform and other software platforms?

The A2:java platform is a pure software platform running on top of other hardware-based platforms (Unix, NT, etc.).


The Java platform consists of two parts: a Java Virtual machine (JVM): A software on a hardware platform, and a byte code (class file) is the machine language of the JVM. Java application programing Interface (API)--Series of class files written in the Java language that run on the JVM.


What are the differences between q3:c++ and Java?

A3: Although both Java and C + + use similar syntax and are object-oriented programming languages, Java does not support pointers. Pointers are inherently more slippery and annoying. Java does not support multiple inheritance because it brings more trouble than it solves. Instead, Java supports multiple interface inheritance, running a class to inherit (implement) multiple method signatures from different interfaces. Multi-Interface inheritance runs the behavior of subclass objects with polymorphism. The destructor is not supported in Java, but the Finalize () method is added. The garbage collector calls its finalize () method before reclaiming objects, but we do not know when the object is reclaimed. We should not use finalize to free resources, such as file processing, sockets, database connections, because these resources are limited and we do not know when the Finalize () method is invoked. Java has no structure or federation, because traditional data structures are designed as an object-oriented framework (the Java Collection Framework).

What is the effect of Q4:java packages.

A4: There can be classes with the same name under multiple packages, so the package can help us solve the naming conflict problem. In addition, it can organize files well. For example, the java.io package is related to I/O and the java.net packet is network-related, and so on. If we put all the. java files in a single package, it will be difficult to manage these files as the project grows.

We can use package to declare the package where the class resides. The package keyword should be the first keyword that appears in the source code, followed by the import statement. By default, Java.lang packages are introduced implicitly, and other packages must be explicitly introduced.

Package com.xyz.client; 
Import   java.io.File; 
Import   

Q5: Explain Java class loader (class loader)? If you have a class in a package, what do you need to do to run it? Explain dynamic class loading?

A5: class loader is graded. The class is loaded into the JVM when it is already running in the JVM and the class references the name of a class. So, how does the first class get loaded? The first class to be loaded by the JVM is the class that declares the main () method and is run. Then all of the loaded classes are raised by it--it's already in the JVM and running.

A class loader creates a namespace. All JVMs have at least one class loader, called the Raw class loader or boot class loader, which is implemented by C or C + +. In addition, Java allows class loaders to be loaded in the Java language for loading class files. Typically, the JVM provides two such non-primitive ClassLoader: The Boot class loader (Bootstrap), which loads the class file (Java.* package) inside the JDK, typically loads the Rt.jar and I18n.jar Extension class loader (Extensions)- Load the jar bundle under the JDK extension path, java.ext.dirs the path specified by the system variable, typically the JRE's Lib/ext Path System class loader (System)-Loads the class file from the system Classpath, and the system classpath by The Java.class.path system variable is determined by specifying the –classpath/–CP parameter of the CLASSPATH environment variable or the command line


The class loader is hierarchical and uses a delegate model. The class loader first requests its parent class loader to load the class file, if the parent loader fails to load, and then loads itself. Once the parent loader has loaded the class, the child loader does not load repeatedly. Classes loaded by the child loader can access classes that are loaded by the parent loader, but they do not, in turn, be true.

Q: A class with the main () method under a package, what you need to do to run it.

For example: You have a class called "Pet" in the engineering file "C:\myProject" and the package name is "Com.xyz.client", can you compile and run it in the following way?

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.Pet

The answer is no. You will get this exception: "Exception in Thread" main "Java.lang.-noclassdeffounderror:com/xyz/client/pet".
You need to set CLASSPATH to set the operating system's "CLASSPATH" environment variable, including the project file "C:\myProject" Setting the operating System "CLASSPATH" environment variables, including engineering files "C:\myProject\client.jar ", this jar package should contain Pet.class class files. Run the command line to provide-CP or-classpath environment variables:

C:\>java  -cp  c:/myproject  com.xyz.client.Pet   
              OR   
c:\>java-classpath c:/myproject/ Client.jar  Com.xyz.client.Pet

Q: Explain static vs dynamic class loading.

Static class loading
Dynamic class Loading
static loading of classes accompanies the Java new operator

class MyClass {
    public static void Main (String args[]) {
&nbs p;        Car c  = new  car (); &NBSP
   }
}


dynamic loading is a programming technique that invokes the class loading feature at run time.
//static method which returns a Class
Class.forName (String className); The static method above

Returns a class instance associated with the class name. The string "ClassName" can be provided dynamically at run time. Unlike static loading, dynamic loading decides to load car class files or Jeep class files based on attribute files or other run-time conditions. Once a class is loaded, you can create an instance of the class in the following ways (like invoking an parameterless constructor with the new operator).

Class.newinstance ();//a non-static method, which creates A instance of A  
    & nbsp;                                //class (i.e. creates) object) .   
Jeep myjeep = null;
//myclassname should is read from a. properties file or a Constants class.  
//stay away from hard coding V Alues in your program.   
String myclassname = "Au.com.Jeep";
Class vehicleclass = Class.forName (myclassname);
Myjeep = (Jeep) vehicleclass.newinstance ();
Myjeep.setfuelcapacity;
The noclassdeffoundexception exception will be thrown if a class is used with the new operator, but the referenced class is not found in the runtime system.
ClassNotFoundException will be thrown if the program tries to load a class with a string class name in one of the following ways, and this class is undefined.

The Forname (..) method in Class-class.
The Findsystemclass (..) method in Class-classloader.
The LoadClass (..) method in Class-classloader.


Q: Static initialization and static block is what.

A: When a class is loaded, its static block is executed, which is preceded by the constructor execution. As its name, it is used to initialize static members.

public class Staticinitializer {public 
   static final int A = 5; 
   public static final int B; The It is notæpublic static final int B = null; 
   The since B is final, the it can be initialized only once. 
  
   Static initializer block, which are executed only once the class is loaded.  
    
   static { 
      if (A = = 5) 
         B = ten; 
      else 
         B = 5;    
   } 
 
   Public Staticinitializer () {}  //constructor was called only after static initializer block 

The following code will be output: a=5, b=10

public class Test { 
    System.out.println ("A =" + Staticinitializer.a + ", B =" + staticinitializer.b); 
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.