Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Java Language Works

Tags:

java

jvm

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-code 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?


2 Answers

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.

like image 148
maaartinus Avatar answered Mar 25 '26 06:03

maaartinus


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.

like image 40
ayush Avatar answered Mar 25 '26 04:03

ayush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!