In Java Purpose of class file

I tried to understand How class files work in Java and what their purpose is I found some information on the Internet, but I got an unsatisfactory explanation

Once we run the compiler, we get Class file, which is bytecode Is this machine readable? If not, that's why we need the interpreter of the program to run successfully?

In addition, due to The class file is equivalent to our Java program, why can't someone run it by using VM Class files run Java programs directly, and they also need to have Java files?

Solution

By definition, a JVM is a virtual machine, which is a software machine that simulates the operation of a real machine Like a real machine, it has an instruction set (bytecodes), a virtual computer architecture and an execution model It can run code written with this virtual instruction set, just as a real machine can run machine code

Therefore, the class file contains the instructions in the virtual instruction set and can run them In this regard, the virtual machine can interpret the code itself or compile for the current running hardware architecture Some people do two things, others do one of them

For example, Java hotspot initially interprets bytecode and gradually compiles the code into machine code This is called adaptive optimization Some virtual machines are always compiled directly into machine code

Therefore, you can see that there are two different "compilation concepts" One is to convert java code into JVM bytecode (from. Java to. Class) The second compilation phase occurs when the program is running, in which bytecode can be interpreted or compiled into actual machine code This is done by the just - in - time compiler in the JVM

Therefore, as you can see, a computer cannot run a java program directly because it is not written in a language understood by the computer It is written in a language that all JVM implementations can understand And there are many JVM implementations of operating system and hardware architecture These JVMs use this common language (bytecode) to convert programs for any specific hardware (machine code) This is the beauty of virtual machines

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>