java JVM 是生成字节码还是运行字节码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32011039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does JVM generate bytecode or run bytecode?
提问by Mohamed Seif
I'm little confused here, does the JVM represents the bytecode (generate it) or it's just it load the compiled .class files (bytecode) into memory?! or JVM is just specifications to run the bytecode in a platform independent way?! thank you very much.
我在这里有点困惑,JVM 是代表字节码(生成它)还是只是将编译后的 .class 文件(字节码)加载到内存中?!或者 JVM 只是以独立于平台的方式运行字节码的规范?!非常感谢你。
回答by appleseedexm
The Java compiler (javac) turns your human-readable code into bytecode, which is then running in a JVM.
Java 编译器 (javac) 将人类可读的代码转换为字节码,然后在 JVM 中运行。
From the oracle docs:
来自oracle 文档:
A program has to be converted to a form the Java VM can understand so any computer with a Java VM can interpret and run the program. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM.
程序必须转换为 Java VM 可以理解的形式,以便任何具有 Java VM 的计算机都可以解释和运行该程序。编译 Java 程序意味着将程序文件中的程序员可读文本(也称为源代码)转换为字节码,字节码是 Java VM 平台无关的指令。
回答by M3579
When you say javac file
, the Java Compiler (called javac) will convert your code into an intermediate form (bytecode). It does not convert it directly to machine language, which is platform specific, so that you can give the class files (bytecode) to anyone on any platform. This is how "Write once, Run anywhere" works. Instead of compiling to platform specific machine language, it compiles to a generic bytecode.
当您说 时javac file
,Java 编译器(称为 javac)会将您的代码转换为中间形式(字节码)。它不会直接将其转换为特定于平台的机器语言,因此您可以将类文件(字节码)提供给任何平台上的任何人。这就是“一次编写,随处运行”的工作原理。它不是编译为特定于平台的机器语言,而是编译为通用字节码。
When you say java file
, the JVM will take the bytecode and convert it to native machine language in chunks "on the fly" (during runtime) and execute them. It does this using a JIT compiler (which might be a source of confusion, since this is NOT the same as javac). By the way, the JDK is not the same as javac. The JDK is an SDK (software development kit) that contains everything in Java, including the JVM and javac.
当您说 时java file
,JVM 将获取字节码并将其“即时”(在运行时)以块的形式转换为本地机器语言并执行它们。它使用 JIT 编译器(这可能是混淆的根源,因为它与 javac 不同)来做到这一点。顺便说一下,JDK 与 javac 不同。JDK 是一个 SDK(软件开发工具包),包含 Java 中的所有内容,包括 JVM 和 javac。
回答by Sasmita Nayak
- JVM = JIT Compiler + Java Interpreter + Garbage Collector
- JRE = JVM + Library Classes
- JDK = JRE + Development Tool
- JVM = JIT 编译器 + Java 解释器 + 垃圾收集器
- JRE = JVM + 库类
- JDK = JRE + 开发工具
Sun JVM is written in C and Oracle JVM is written in C++
Sun JVM 是用 C 编写的,Oracle JVM 是用 C++ 编写的
Java compiler javac converts source code into bytecode. JIT Compiler and Java Interpreter inside JVM convert the bytecode into corresponding machine code.
Java 编译器 javac 将源代码转换为字节码。JVM 内部的 JIT Compiler 和 Java Interpreter 将字节码转换为相应的机器码。
In java, only the source code(.java files) and bytecodes(.class files) are available. And we can't save the machine codes(.exe files) as because .exe files can only be formed at Run Time and vanished from RAM as soon as the program is executed completely.
在 java 中,只有源代码(.java 文件)和字节码(.class 文件)可用。我们不能保存机器代码(.exe 文件),因为 .exe 文件只能在运行时形成,并在程序完全执行后立即从 RAM 中消失。
In our system both the javac.exe(for compiling java source code, eg: javac HelloWorld.java) and java.exe(for executing java bytecode by JVM, eg: java HelloWorld) are called, which are available in .exe formats only(javac.exe and java.exe). So the Java Compiler javac and JVM were not written in Java.If those were written in Java then those might have been available in javac.class and java.class format.
在我们的系统中,javac.exe(用于编译java源代码,例如:javac HelloWorld.java)和java.exe(用于JVM执行java字节码,例如:java HelloWorld)都被调用,它们仅在.exe格式中可用(javac.exe 和 java.exe)。因此,Java 编译器 javac 和 JVM 不是用 Java 编写的。如果它们是用 Java 编写的,那么它们可能以 javac.class 和 java.class 格式提供。
javac comes under JDK and not under JVM.Remember,JVM only works during Run Time means after the compilation of Source Code into Byte Code..but before that javac compiles the source code into byte code. JVM converts bytecode into corresponding machine code by JIT Compiler and Java Interpreter.
javac 属于 JDK 而不是 JVM。请记住,JVM 仅在运行时工作意味着在将源代码编译为字节码之后..但在此之前 javac 将源代码编译为字节码。JVM 通过 JIT Compiler 和 Java Interpreter 将字节码转换为相应的机器码。
For different Operating Systems different JDK and JRE softwares are available by Oracle Corporation; So both the JVM(coming under JRE) and javac Compiler(coming under JDK) are Platform Dependent.So it is confirmed that javac Compiler and JVM are not written in Java.Because Java language is always Platform Independent.
对于不同的操作系统,Oracle 公司提供了不同的 JDK 和 JRE 软件;所以JVM(JRE下)和javac编译器(JDK下)都是Platform Dependent。所以可以确定javac Compiler和JVM不是用Java编写的。因为Java语言总是Platform Independent。
回答by Dragan Bozanovic
JVM runs the bytecode, Java compilergenerates it.
JVM 运行字节码,Java 编译器生成它。
However, applications can generate the bytecodewhile they run, but the generated bytecode is again executed by the JVM.
然而,应用程序可以生成字节码,而它们运行,但生成的字节码由JVM再次执行。
回答by Rohit sharma
JVM, depending on platform , convert the byte code to m/c code. More precisely, JIT (just-in-time) compilerinside JVM does this. Byte code is generate by javac.exe. And java.exe converts this byte code to m/c code with the help of jvm.dll(in windows).
JVM,根据平台,将字节码转换为 m/c 码。更准确地说,JVM 中的JIT(即时)编译器就是这样做的。字节码由 javac.exe 生成。并且java.exe 在jvm.dll(在windows 中)的帮助下将此字节码转换为m/c 代码。
回答by abhiabhi
JDK(javac) generates the byte code(.class files). Now this byte code can be run on any platform by the JVM of that platform.
JDK(javac) 生成字节码(.class 文件)。现在这个字节码可以通过该平台的 JVM 在任何平台上运行。
回答by Navanee Subburaj
enter image description hereThere is an inbuilt tool now available called Java Visual VM(after JDK version 6, update 7) which you will find in bin, the directory where javac.exe exists. Just open this tool which monitors processes that use JVM (profiler). It will show javac,eclipse etc as a Java process running on JVM if you execute it while Visual VM is open.
在此处输入图像描述现在有一个名为 Java Visual VM(JDK 版本 6,更新 7 之后)的内置工具,您可以在 bin(javac.exe 所在的目录)中找到它。只需打开这个工具来监控使用 JVM(分析器)的进程。如果在 Visual VM 打开时执行它,它会将 javac、eclipse 等显示为运行在 JVM 上的 Java 进程。
回答by Vrushali Shirodkar
Jdk generates the byte code for a given file with .java extension. JVM converts the byte code into machine language and then executes it.
Jdk 为扩展名为 .java 的给定文件生成字节码。JVM 将字节码转换成机器语言,然后执行。
回答by prime
In short Java Virtual Machine runs / interprets / translates Bytecode into native machine code. It does not generate the bytecode.And we can consider JVM as an interpreter.
简而言之,Java 虚拟机运行 / 解释 / 将字节码转换为本地机器码。它不生成字节码。我们可以将 JVM 视为解释器。
Below will help to understand the above,
下面将有助于理解上述内容,
Bytecode, also termed portable code or p-code, is a form of instruction set designed for efficient execution by a software interpreter. It is something in between a human readable source code and a machine readable machine code. A bytecode program may be executed by parsing and directly executing the instructions, one at a time. This kind of bytecode interpreter is very portable. Some systems, called dynamic translators, or just-in-time (JIT) compilers, translate bytecode into machine code as necessary at runtime. This makes the virtual machine hardware-specific, but doesn't lose the portability of the bytecode. Refer this.
字节码,也称为可移植代码或 p 代码,是一种指令集形式,旨在由软件解释器高效执行。它介于人类可读的源代码和机器可读的机器代码之间。字节码程序可以通过解析并直接执行指令来执行,一次一个。这种字节码解释器非常便携。一些称为动态翻译器或即时 (JIT) 编译器的系统在运行时根据需要将字节码翻译成机器码。这使得虚拟机特定于硬件,但不会失去字节码的可移植性。参考这个。
A compiler is computer software that transforms computer code written in one programming language (the source language) into another programming language(the target language). A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java class files containing platform-neutral Java bytecode.
甲编译器是计算机软件写入一个编程语言(源语言)向另一种编程语言的变换的计算机代码(目标语言)。Java 编译器是编程语言 Java 的编译器。Java 编译器最常见的输出形式是包含与平台无关的 Java 字节码的 Java 类文件。
For Java it has a dedicated compiler called Javac. Javac Compiles your Java code into Bytecode. And then we have the JVM which uses that bytecode.
对于 Java,它有一个称为 Javac 的专用编译器。Javac 将您的 Java 代码编译为 Bytecode。然后我们有使用该字节码的JVM。