Four core technical ideas in Java

Source: Internet
Author: User

Java has become a huge and complex technology platform. for developers, it is essential to have a better grasp of Java technology and have a deep understanding of the underlying technical processing details. Thoughts on core concepts
Understanding can help us draw inferences from each other and improve our understanding of the entire Java platform. This section describes several core concepts of the Java technology platform, including the ideas contained in them.
It helps us to have a deeper understanding of Java technology.

  Java Virtual Machine

The main task of the Java Virtual Machine is to install the class file and execute the bytecode. The Java Virtual Machine contains a class loader that can load class files from programs and APIs. In Java APIs, only the classes required for program execution are loaded. Bytecode is executed by the execution engine.

Different Java virtual machines may implement different execution engines. In virtual machines implemented by software, the simplest execution engine is to interpret bytecode at one time. Another execution engine is faster,
But it also consumes more memory, called "instant Compiler (just-in-time
Compiler )". In this case, the bytecode that is executed for the first time will be compiled by the local machine code. The locally compiled machine code is cached and can be reused when the method is called later. The
The three execution engines are adaptive optimizers. In this method, the virtual machine starts to interpret bytecode, But it monitors the activity of running programs and records the most frequently used code segments. When the program is running,
The Virtual Machine only compiles the Code with the most frequent activities at the cost. Other codes are retained as bytecode because they are not frequently used. The virtual machine will continue to explain them. An adaptive optimizer can make
Java Virtual Machine in 80% ~ Execute optimized local code in 90% of the time, and only need to compile 10% ~ 20% of the Code that has an impact on performance.

When the Java Virtual Machine is implemented by software on the host operating system, the Java program calls the native
Method. There are two methods in Java:
Java method and local method. The Java method is compiled by the Java language and compiled into a bytecode file, which is stored in the class file. The local method is composed of other languages (such as C, C ++, or assembly ).
Language. The local method is saved in the dynamic link library. The format is proprietary to each platform. When a running Java program calls a local method, the VM load contains
And call this method. The local method is to connect the Java program to the underlying host operating system.

Using a local method, Java programs can directly access resources of the underlying operating system. Java Native Interface (JNI)-enables local methods to run on any Java platform of a specific host system.

If you want to use resources on a specific host and they cannot be accessed from the Java API, you can write a Java program related to the platform to call the local method. To ensure platform independence of the program, you can only access underlying system resources through Java APIs.

  Architecture of the Class Loader

A Java application can use two types of loaders: the "Bootstrap" Class Loader and the user-defined class loader. Start the Class Loader (this is the only one in the System)
Is part of Java Virtual Machine implementation. The start class loader usually loads classes from the local disk by some default method, including Java
API class (the start class loader is also called the original Class Loader, system class loader, or default Class Loader ).

Java applications can install user-defined class loaders at runtime, which can be loaded in a custom manner. For example, download the class file from the network. Start
The class loader is the essential part of Virtual Machine implementation. The user-defined class loader is not, but the user-defined class loader can be written in Java and compiled into a class file, can be installed by virtual machines
And can be instantiated like other objects.

Because there is a user-defined class loader, you do not have to compile it to know all the classes that will be added to the running Java application. The user-defined class loader makes the extension
Java applications become possible. When it runs, the application can solve what extra classes it needs and decide whether to use one or more user-defined class loaders for loading. Because the class loader is used
So any style that can be expressed in Java code is used for class loading. These classes can be downloaded over the Internet, obtained from some databases, or even dynamically generated.

When a class is loaded, the Java Virtual Machine monitors the class to see whether the class loader is started or loaded by the user-defined class loader. When the loaded class references another class
The VM uses the class loader that loads the first class to load the referenced class. For example, if a virtual machine uses a specific class loader to load the volcano class, it will use this class loader to load
All classes used by the volcano class.

Because the Java Virtual Machine loads classes in this way, by default, only other classes loaded by the same class loader can be seen. In this way, the Java architecture allows multiple namespaces to be created in a Java application. Each class loader in the Java program at runtime has its own namespace.

How many (or how many) classes can be created by a Java application and loaded by different classloaders are stored in different namespaces. They cannot access each other, unless the application explicitly allows this
. When writing a Java application, classes loaded from different source files can be separated in different namespaces. With this method, you can use the architecture of the Java class loader to control
What are the mutual influences between the codes loaded in different source files? In particular, they can prevent malicious code from obtaining access or damaging the permissions of malicious code.

The Web browser is an example of dynamic expansion. The web browser uses the user-defined class loader to download from the network for Java
The class file of the applet. The Web browser uses a Java application to install the user-defined class loader. This user-defined class loader is usually called Java
The Applet Class Loader knows how to request a class file from the HTTP server. Java
The applet can be used as an example of dynamic expansion, because the Java application does not know when it will download the class file requested by the browser from the network. Only when the browser encounters a Java
The applet page determines whether to download the class file.

Java applications started by Web browsers usually create different user-defined class loaders for each network address that provides class files. Therefore, different user-defined class loaders
Class files from different sources. In this way, they can be placed under different namespaces of Java host applications. Java
The applet files are placed in different namespaces, and malicious Java
The applet code will not directly access the class files downloaded from other places. This restricts or prevents code from accessing each other from different sources.

 

Java class file

Java
The class file mainly makes Java more suitable for the network in terms of platform independence and Network Mobility. In terms of platform independence, it provides a binary form independent of the underlying host platform for Java programs.
. This approach breaks the tradition of C, C ++, and other languages. programs written in these traditional languages are usually compiled first, then it is connected to a separate, dedicated support for specific hardware platforms and operations
The binary file of the system. Generally, binary executable files on one platform cannot work on other platforms. Java class files can run binary files on any hardware platform and operating system that supports Java virtual machines.

When compiling and connecting a C ++ program, the obtained executable binary file can only run on the specified hardware platform and operating system, this binary file contains the machine language of the target processor. The Java compiler translates the commands of the Java source file into bytecode, which is the "machine language" of the Java Virtual Machine ".

Class files are designed to be compact, so they can be quickly transmitted over the network. Second, because Java programs are dynamically connected and dynamically scalable, class files can be used when necessary.
. This feature allows Java applications to schedule the time for downloading class files from the network, thus minimizing the waiting time of end users.

 Java API

By supporting platform independence and security, Java APIs adapt Java to network applications. Java
An API is a collection of runtime libraries. It provides a set of standard methods for accessing host system resources. Java API used by the class file of the virtual machine to load the program when running the Java program
Class file. All mounted class files (including from the application and from Java
And all loaded dynamic libraries (including local methods) constitute the entire program that runs on the Java Virtual Machine.

Before a platform can support Java programs, it is necessary to explicitly implement the API functions on this specific platform. To access local resources on the host, Java
The API calls a local method. Because the Java API class file calls local methods, the Java program does not need to call them any more. In this way, Java API
The class file provides a Java program with platform independence and standard interfaces for the underlying host. For Java programs, regardless of the Internal Platform, Java
APIs have the same performance and predictable behavior. It is precisely because the Java Virtual Machine and Java
Therefore, Java programs can become platform-independent programs.

Java API also contributes to the Java security model. Before the Java API method performs any potentially dangerous operations (such as local disk write operations), It queries the access controller to check whether authorization is obtained. An access controller is a class used to perform stack checks and has decided whether to allow certain operations.

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.