How Java Virtual machines work

Source: Internet
Author: User

First, what is a Java virtual machine
The JAV virtual machine is an imaginary machine that is implemented by software simulations on the actual computer. A Java Virtual machine has its own imaginary hardware, such as a processor, stack, register, and a corresponding specified system.
Summary: Java Virtual machine-processor, stack, register, instruction system.
Second, why use Java Virtual machine
A very important feature of the Java language is its independence from the platform. The use of Java virtual machines is the key to achieving this feature. General high-level languages if you want to run on a different platform, you need to compile at least a different target code. When the Java language virtual machine is introduced, the Java language does not need to be recompiled when it runs on different platforms. The Java Virtual machine masks information that is specific to the platform, so that the Java language compiler simply generates the target code (bytecode) that runs on the Java Virtual machine and can run unmodified on multiple platforms. When a Java virtual machine executes a bytecode, it interprets the bytecode as a machine instruction execution on a specific platform.
Summary: Java platform agnostic, compile object code into byte code
Third, the life cycle of the Java Virtual machine
A running Java Virtual machine has a clear task: Executing a java program. It runs when the program starts executing, and it stops at the end of the program. If you run three Java programs at the same time, there will be three running Java virtual machines.
The Java Virtual machine always starts with a main () method, which must be public, return void, and receive a string array directly. When the program executes, you must indicate to the Java virtual machine that the package contains the class name of the main () method.
The Main () method is the beginning of the program, which is executed by the thread initialized to the program's initial thread. The other threads in the program are started by him. There are two types of threads in Java: The daemon thread (daemon) and the normal thread (Non-daemon). A daemon thread is a thread that the Java virtual machine uses itself, such as a thread that is responsible for garbage collection, which is a daemon thread. Of course, you can also set your own program as the daemon thread. The initial thread that contains the main () method is not a daemon thread.
As long as there are normal threads executing in the Java virtual machine, the Java Virtual machine will not stop. If you have sufficient permissions, you can call the exit () method to terminate the program.
Summary: The Main () method is the start of the program, the daemon thread, and the normal thread, sufficient permission to call exit () to terminate the program

Third, the architecture of the Java Virtual machine
A series of subsystems, memory regions, data types, and usage guidelines are defined in the specification of a Java virtual machine. These components constitute the internal structure of the Java Virtual machine, and they not only provide a clear internal structure for the implementation of the Java Virtual machine, but also strictly stipulate the external behavior of the Java Virtual Machine implementation.
Each Java Virtual machine consists of a class loader subsystem (class loader subsystem), which is responsible for loading the types (classes and interfaces Interface) in the program and giving them unique names. Each Java Virtual machine has an execution engine (execution engine) responsible for executing the instructions contained in the loaded class.
Summary: Class loading subsystem, execution engine
The execution of a program requires a certain amount of memory space, such as bytecode, additional information about the loaded class, objects in the program, parameters of the method, return values, local variables, intermediate variables to process, and so on. The Java Virtual machine stores this information in the data area. Although the implementation of each Java virtual machine contains a data area, the Java Virtual Machine specification is very abstract about the data area provisions. Many of the structural details are left to the Java Virtual Machine implementation to play by itself. The memory structures on different Java Virtual machine implementations vary widely. Some implementations may consume a lot of memory, while others may consume little memory; Some implementations may use virtual memory, while others do not. This relatively refined Java Virtual Machine Memory specification enables Java Virtual machines to be implemented on a wide range of platforms.
Part of the data area is common to the entire program, and other parts are controlled by separate threads. Each Java Virtual machine contains a method area and heap, which are shared by the entire program. After a Java virtual machine loads and parses a class, the information parsed from the class file is saved in the method area. Objects that are created when the program executes are saved in the heap.
When a thread is created, it is allocated only to its own PC register "PC Register" (program counter) and Java stack (Java stacks). When a thread does not call the local method, the next instruction that the thread executes is saved in the PC register. The Java stack holds a state when a thread invokes a method, including local variables, parameters that invoke methods, return values, and intermediate variables to process. The state when the local method is called is saved in the local methods stack (native method stacks), possibly in registers or other non-platform independent memory.
The Java stack consists of a stack frames (or frames). The stack block contains the state of the Java method call. When a thread calls a method, the Java virtual Opportunity presses a new block into the Java stack, and when the method runs at the end, the Java Virtual opportunity pops up the corresponding block and discards it.
The Java Virtual machine does not use registers to hold the computed intermediate results, but instead uses the Java stack to store the intermediate results. This is the Java Virtual Machine instructions are more compact and easier to implement a Java virtual machine on a device without a register.
Summarize:the stack area is a local variable. The method area is the type information that holds the class, including the static variables and methods of the class
Heap: Storing class objects

Iv. data type (Types)
All data used in a Java virtual machine has a deterministic data type, and the data types and operations are strictly defined in the Java Virtual Machine specification. Data types in Java are divided into raw data types (primitive types) and reference data types (reference type). The reference type relies on the actual object, but not the object itself. Raw data types do not depend on anything, they are the data they represent.
The original data type in all Java programming languages is the original data type of the Java Virtual machine, except for the Boolean type (Boolean). When the compiler compiles the Java source code into bytecode, it uses an integer (int) or byte type (byte) to represent the Boolean type. Use integer 0 in a Java virtual machine to denote the Boolean false, to use a nonzero integer to denote the true of the Boolean, and to represent the Boolean array as a byte array, although they may be stored in a byte array or byte block (bit fields) in the heap. Except for the Boolean type, the other primitive types are the data types in the Java Virtual machine. Data types in Java are divided into: shaped Byte,short,int,long;char and floating-point float,double. The data types in the Java language have the same scope on any host.
In a Java virtual machine, there is also a primitive data type that cannot be used in the Java language----return value type. This type is used to implement the "finally classes" in the Java program, as described in "finally classes".
The reference type may be created as: class type, interface type (interface type), and array type. They all refer to objects that are created dynamically. When a reference type references null, the description does not reference any objects.

Range Code
  1. Typerange
  2. Byte8-bit signed, complement Integer (- 1, inclusive)
  3. Short16-bit signed-S complement integer (-215 to 215- 1, inclusive)
  4. Int32-bit signed-S complement integer (-231 to 231- 1, inclusive)
  5. Long64-bit signed-S complement integer (-263 to 263- 1, inclusive)
  6. Char16-bit unsigned Unicode character (0 to 216- 1, inclusive)
  7. Float32-bit IEEE 754 single-precision float
  8. Double64-bit IEEE 754 double-precision float
  9. Returnvalueaddress of an opcode within the same method
  10. Referencereference to a object on the heap, or null


Summary: Raw data types and reference data types. Boolean:0-false, non-zero integer-true
Five, byte length
The smallest data unit in a Java Virtual machine is word, and its size is defined by a Java virtual machine. But a word must hold byte, short, int, char, float, returnvalue, reference; Two words must be sufficient to accommodate long,double. Therefore, the virtual machine's implementation must provide at least a word that is less than 31bits, but it is best to choose the most efficient word length on a particular platform. At run time, the Java program cannot determine the length of the machine being run. Word length does not affect the behavior of the program, he is just a representation in a Java virtual machine.
Summary: The smallest data unit in a Java Virtual machine is a word
Six, class loader subsystem

Reprint Source: http://shenyuc629.iteye.com/blog/1698770

How Java Virtual machines work

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.