用于Java中的.class文件的目的 [英] Purpose for .class files in Java

查看:108
本文介绍了用于Java中的.class文件的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解.class文件如何在Java中工作以及它们的目的是什么.我在网上找到了一些信息,但是却得到了令人不满意的解释.

I am trying to understand how .class files work in java and what's their purpose. I found some information online, but I get unsatisfying explanations.

运行编译器后,我们将立即获得.class文件,该文件为字节码.这台机器可读吗?如果不是,这就是为什么我们需要解释器才能使程序成功运行?

As soon as we run the compiler we get the .class file, which is bytecode. Is this machine readable or not? And if not, this is why we need the interpreter for the program to run successfully?

此外,由于.class文件与我们的.java程序等效,所以为什么不能有人仅通过使用VM运行.class文件来立即运行Java程序,而他们需要将.java文件作为出色地?

Also, since the .class file is the equivalent of our .java programs, why can't somebody run a java program straight away by just running the .class file using VM and they would need to have the .java file as well?

推荐答案

根据定义,JVM是虚拟的机器,即模拟真实机器运行的软件机器.与真实机器一样,它具有指令集(

The JVM is by definition a virtual machine, that is a software machine that simulates what a real machine does. Like real machines it has an instruction set (the bytecodes), a virtual computer architecture and an execution model. It is capable of running code written with this virtual instruction set, pretty much like a real machine can run machine code.

因此,类文件在虚拟指令集中包含指令,并且能够运行它们.为此,虚拟机可以解释代码本身,也可以针对其当前正在运行的硬件体系结构对其进行编译.有些会同时执行,有些只会执行其中一个(例如,.net运行时在第一次调用该方法时就会编译一次).

So, the class files contain the instructions in the virtual instruction set, and it is capable of running them. For that matter, a virtual machine can either interpret the code itself or compile it for the hardware architecture it is currently running. Some do both, some do just one of them (e.g. .net runtime compiles once the first time the method is called).

例如,Java HotSpot最初会解释字节码,然后将其逐步编译为机器代码.这称为自适应优化.一些虚拟机总是直接编译为机器代码.

For instance, the Java HotSpot initially interprets bytecodes, and progressively compiles the code into machine code. This is called adaptive optimization. Some virtual machines always compile to machine code directly.

因此,您可以看到有两个不同的编译概念".一种是将Java代码转换为JVM字节码(从.java到.class).程序运行时发生第二个编译阶段,其中字节码可以解释为或编译为实际的机器代码.这是通过JVM中的即时编译器完成的.

So, you can see there are two different "compiling concepts". One consists in the transformation of Java code to JVM bytecodes (From .java to .class). And a second compilation phase happens when the program runs, where the bytecodes may either be interpreted or compiled to actual machine code. This is done by the just-in-time compiler, within the JVM.

因此,如您所见,计算机无法直接运行Java程序,因为该程序不是以计算机可以理解的语言编写的.所有JVM实现都可以用通用语言编写.并且该JVM的实现适用于许多操作系统和硬件体系结构.这些JVM以这种通用语言(字节码)的形式将程序转换为任何特定的硬件(机器代码).那就是虚拟机的美.

So, as you can see, a computer cannot run a Java program directly because the program is not written in a language that the computer understands. It is written in lingua-franca that all JVM implementations can understand. And there are implementations of that JVM for many operating systems and hardware architectures. These JVMs translate the programs in this lingua-franca (bytecodes) for any particular hardware (machine code). That's the beauty of the virtual machine.

这篇关于用于Java中的.class文件的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆