Виртуальная машина Java генерирует 0 и 1?

262
Foo

Извините, это очень базовый уровень информатики. Виртуальная машина Java генерирует 0 и 1 или код ассемблера? Если он генерирует код на ассемблере, к процессору присоединен ассемблер, который преобразует это в 0 и 1? Или, если он генерирует 0 и 1, нужен ли процессору ассемблер? Если да, то для чего?

0
Все состоит из 0 и 1. Даже ассемблерный код. Valmiky Arquissandas 9 лет назад 1
Когда вы говорите «0 и 1», это звучит так, как будто вы действительно имеете в виду [машинный код] (https://en.wikipedia.org/wiki/Machine_code). Wyzard 9 лет назад 1

2 ответа на вопрос

2
Jason C

The Java compiler (not the JVM) takes Java source code (which is essentially still just 0's and 1's, but a certain specific set of 0's and 1's that can represent information that is easy for humans to deal with, i.e. readable text) and compiles it to Java bytecode, which is 0's and 1's with a different meaning.

Java bytecode is conceptually like "assembler" except it is platform-independent instructions meant to be interpreted by the Java virtual machine. The JVM acts as a "translator" between Java bytecode and native bytecode (that is, machine instructions for the actual hardware it is running on) - conceptually the same as, say, a Nintendo emulator. It is this that allows compiled Java to run on any platform.

So to answer your question directly: The JVM does not generate Java bytecode; the Java compiler does, and the JVM interprets that bytecode and "makes things happen".

However, in modern times, advanced JVMs do more than just interpret - the "HotSpot" technology that you may have heard of, for example, allows the JVM to dynamically determine what parts of your Java bytecode are running frequently (and thus could benefit from a speed up) and then take those parts and translate them to full native bytecode on the fly (so the original Java bytecode is no longer interpreted, and the new, faster native bytecode is directly run). This all happens transparently to your Java program. Long ago Java used to take a lot of criticism for being "slow", but those days are mostly gone with the advent of HotSpot and similar techniques.

So in a sense, modern JVMs do generate "0s and 1s", in that they do generate additional native code from the input Java instructions; but be wary of referring to data as "0s and 1s" - every piece of digital data is "0s and 1s" (often organized into groups of 8 binary digits called a "byte"), from machine code to program source code to text to images to MP3s to whatever.

You can find a nice writeup of how the JVM works here.

For very concrete specifications of the Java language, Java bytecode, and how the compiler and virtual machine are required to behave, both the language and virtual machine specification can be found here.

0
kevin628

It generates java bytecode, which is a special instruction set just for the JVM. It is comparable to but not the same as native machine code (zeros and ones).

See here: Java bytecode (Wikipedia) (scroll down to the Example section).