• Post Reply Bookmark Topic Watch Topic
  • New Topic

Execution of Java programs  RSS feed

 
Arpa Mukherjee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very keen to know that whether the JAVA programs are executed by the JVM running in my environment or the processor of my system. I find it very confusing as in many books it is written that java programs are executed by the JVM. I can understand that the JVM is interpreting the compiled "bytecode" but whether it even does the job of processing or not isnt clear to me. If so then how does the JVM performs the role of a processor??
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ultimately, all code that runs on your computer runs on the processor, directly or indirectly. The JVM itself is also just a program which runs on the processor of your computer.

Programs written in for example C or C++ are normally compiled directly to machine code instructions that your CPU can execute directly. Java code is compiled to byte code, which is like machine code, but not machine code specifically for a certain kind of CPU such as an Intel x86 CPU, but for the Java virtual machine.

When you run your compiled Java program, the JVM reads the byte code instructions and executes them one by one, just like a normal CPU would - but it does it in software instead of directly on the hardware.

This was how Java worked until version 1.0 or 1.1 (before 1998 or so). Since then, a lot of sophisticated features have been added to the JVM to make your Java program run fast. The most important of these is the JIT (Just-In-Time) compiler. When your Java progam runs, it takes the byte code, translates it into native machine code for your CPU, and then runs that on the CPU, instead of interpreting and executing the byte code instructions one by one.

Besides the JIT compiler the JVM contains many sophisticated optimization features to make your code run fast.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!