From the Java Main method

Source: Internet
Author: User

When you just touch the Java language, it's a Java main method. We know that this program can run, but how the program is running, we do not know.

It is well known that when executing a Java program, a JVM virtual machine process starts first, and the JVM process dies when the program finishes executing. Other things that cause the JVM process to die are the following:
    1. System.exit (int) method, the virtual machine leg is fine when the method is executed. The int parameter is a status code of 0 o'clock and exits normally if not 0.
    2. When an exception or error is encountered. If an exception is encountered during the procedure, the exception is thrown to the main function without processing, and if the main function is not processed, it is thrown to the JVM, and the JVM crashes if the JVM cannot handle the exception.
    3. The platform on which the JVM depends has an error.
The JVM consists primarily of a class loading subsystem, a runtime data area (memory space), an execution engine, and a local method interface. The runtime data area is also composed of method area, Heap, Java stack, PC register, and local method stack. In memory space, the method area and stack are shared by all Java threads, while the Java stack, the local method stack, and the PC register are owned by each thread. The process of class loading subsystem has been discussed in the previous blog http://blog.sina.com.cn/s/blog_ae63f79a0102vw94.html when executing Java programs. Class loading first loads the. class file from the hard disk into memory, and the. class file and JVM are the keys to the Java implementation once written, running everywhere. The previous point of loading the. class file from the hard disk into memory, where it was loaded, is the method area of the runtime data area described earlier. The JVM then creates a class object in the heap. The class object is created by the JVM itself, and only the JVM creates the class object, and the class object for all classes has only one, which can be said to be an example of a singleton pattern. The following is a discussion of the differences between creating objects through constructors and creating objects through reflection. The difference between newinstance and new is that the object is created differently. The former uses the class loading mechanism. When using newinstance, you must ensure that the class is loaded and connected. The above is implemented by Class.forName (String). Newinstance can only use the default parameterless constructor, and new may use any declared public constructor. It is common to refer to the Java code written, there will be import, using the Import Keyword Java class, the JVM at startup will automatically load the referenced Java class. And we want to be able to load the class dynamically, so we can use Class.forName. So we can get a conclusion: newinstance: Weak type. Low efficiency. Only parameterless constructs can be called.
   NEW: Strongly typed. relatively efficient. Can invoke any public construct.
Newinstance () is an inevitable choice for the implementation of IOC, reflection, interface programming and dependency inversion, and new can only be instantiated for specific classes, not suitable for interface programming.
So when we write JDBC, we find that when we call the database driver, we use the Class.forName () function.

From the Java Main method

Related Article

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.