Java VM in the magic

Java virtual machine contains a lot of cheese point, that which is a bunch of content can be crushed to death>. <

Since everything is an object, everything is a file, that virtual machine class file structure is necessary to understand the.

When all know the origin of programming, writing code is a very strenuous thing, because to let the machine "read" it to understand, people have to write machine code, was developed to a high-level programming language, people easier to write, and that the machine "read" do not understand, you need middle to a "translator" - Java virtual machine.

First-come, Zhang Zhen floor map -

1. The language independence

Not just the Java language, other languages ​​can also be its "translation" of the past. Code come out ~ bytecode

Our slogan is: "write once!"

Write Once, Run Anywhere.

java is not only a platform-independent, and has strong language independence -

Figure:

Java virtual machine and do not care about in the end is how Class files come, they meet the Class file structure format, you will be able to run in a virtual machine.

So what is the class file format?

2. Mysterious class file

Studio to find a project with Android open, free to find a .class files in the appropriate directory structure build, opened in text tool see ~

Encryption!

Seeing things? Take a look at the first sentence is what?

Not wrong, "cafebabe"! ( [Phi dish [Phi ) What signal do? !

key word

  • The basic unit: 8 bytes
  • Binary stream

(Class file is a binary stream to a set of 8-byte unit basis.)

Good to see the structure

Look again control

(1)Magic!

The first value is called "magic", located in the first four bytes .class files, is seen in front of "cafebabe" ~~~

Many articles translated as "magic number", in fact it is the "signal" to indicate the identity of, and the "Yellow River Yellow Sea, Yangtze River I" - to a sense of identity. ~ Without this signal, jvm not know mess!

  • The only role

    It is to determine whether the file is to be recognized by the virtual machine class file

  • value

    Fixed "0xCAFEBABE".

(2) version number (4 bytes)

Behind the magic number is followed by a version number, including minor version number and major version number, each with 2 bytes.

Example, 0033,0000, and 0000 represents the minor version number is the version number is a front 0,0033 51.

(3) the famous constant pool! (Variable bytes)

Who does not know the constant pool, stand up, to ensure that you do not fight -

Can be understood as a warehouse of resources among the Class file, it is the first table type data type Class file appears, is occupied by one of the largest data item Class file space.

Each constant is a constant pool table.

Correspondence table, u2 type, 0x015a represents the number of bytes occupied by the constant pool (to be noted that, the constant pool bytecode from 1 up).

Each constant is a constant pool table, a total of 11 kinds of table structure, they have a common feature is that the first byte (U1) of this type is represented by a table, according to their next byte each type of resolution.

Decompile class to see ~

(4) Access flag (2 bytes)

First look at what has access flag, and each corresponding hexadecimal numbers are?

reference

  1. Getting JAVA virtual machine (1) ----- class file structure
  2. See "HelloWorld" from the bytecode level

Reproduced in: https: //juejin.im/post/5d070b0af265da1b8a4f1d1f

Guess you like

Origin blog.csdn.net/weixin_33834075/article/details/93183512