开发者

How Java Language Works

开发者 https://www.devze.com 2023-02-09 05:42 出处:网络
My knowledge of Java isn\'t great, so I want to ask how the language works. By which I mean not just the \"Language\" but the Virtual Machine as well.

My knowledge of Java isn't great, so I want to ask how the language works. By which I mean not just the "Language" but the Virtual Machine as well.

Here is my understanding.

  1. Java compiler turns code into Java Byte-Code. in the form of a .java file
  2. when the file is run, the JVM reads (just in time) the byte-开发者_如何学JAVAcode and turns it into machine code.
  3. Computer reads the machine code and the program appears to run like a compiled program (to the user).

Is this hopelessly wrong?


There are already many answers, but I'm missing one important point:

"2. when the file is run, the JVM reads (just in time) the byte-code and turns it into machine code."

This is not quite correct.

  • The JVM starts by interpreting the code
  • It looks at the most time consuming parts, the hot spots
  • It analysis the traces, i.e., the typical execution flow
  • It generates machine code optimized for the hot spots and the traces

The less time-consuming parts of code may stay interpreted. If the situation changes (e.g., by loading a new class), some compiled code may show to be not optimal anymore or even incorrect, and it gets thrown away and the JVM reverts to interpreting for a while, then it re-compiles it again.


A Java Virtual Machine (JVM) is the software, which interprets compiled Java byte code and runs the java program. Java Virtual Machine language conceptually represents the instruction set of a stack-oriented, capability architecture.

Java Virtual Machine does not have any information regarding the programming languages. JVM knows only binary byte code format. Programmer can generate the bytecode that adheres to this format in any of the programming languages. Every java program runs within the boundaries defined by the Java Virtual Machine.

The code of java runs inside the JVM cannot go beyond the security constraints defined by Java Virtual Machine. Java applications are considered as secure applications on internet due to this software.


http://en.wikipedia.org/wiki/Java_bytecode

http://en.wikipedia.org/wiki/Java_virtual_machine


Your understanding is correct. I'd like to add the below

  1. The HotSpot compiler also adaptively compiles Java bytecodes into optimized machine instructions


Almost:

  • the Java compiler creates .class files not .java files, which contain the byte code. .java files contain the source code.
  • the JVM (Java virtual machine) is like a (virtual) computer on its own. It interpretes the byte code. The OS only runs the JVM.
  • A JIT (just in time) compiler can compile part of the code to machine code for performance reasons, in which case the JVM delegates the execution of that code to the OS (I guess).


To be precise,

  • When you create a java class, the extension would be .java

  • During compilation, the compiler converts the code (.java file) to .class (byte code).

  • When the code is run, JVM converts the byte code (.class file) to Machine code that can be interpreted by the OS. By doing so, it makes Java as platform independent and JVM as platform dependent.

0

精彩评论

暂无评论...
验证码 换一张
取 消