This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello all, I have a small question. After the .java source file is compiled to a .class file this .class file contains the bytecode. Now this bytecode is verified by the Javaverifier for correctness of the bytecode(correctness of bytecode means checking for viruses in the bytecode, isn't it?) and then the java interpreter changes the bytecode to native code and the jvm executes. Is it that the native code will be sent to the OS by the JVM to interact with the hardware or will the jvm directly interact with the native code.
my understanding is that every machine can only understand it's native code. what the JVM does is translate the bytecode to native. that's why there is a different JVM on every kind of platform (apple, Sun, Linux, etc). Sun took care of all that stuff for us, making development (theoreticaly) easier.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
The JVM has a couple choices. In "interpreter" mode, it looks at a bytecode that maybe says "add 1 to x". It looks up x in a symbol table, finds out where x is in memory, gets the current value, adds one, puts it back. Ok, it's much smarter than that, but you get the idea. The JVM doesn't convert the bytecode to native code, but it is written in native code itself so it's doing all those steps in native code. In "hot spot" mode the JVM actually compiles the bytecode into native code and stores that native code some place. Next time it comes across that statement it executes the native code. Extra cost to compile once, benefit of native code maybe millions of times. Or maybe once. I think some JVMs still profile the code as they run. A JVM migh reason: I've interpreted this code a few thousand times recently, maybe I should compile it. I think that's where the "spot" part of hot spot came from.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Sep 12, 2003
My question exactly is what converts the platform independent bytecode to native code and who executes that native code. is it passed to OS by the JVM to execute or the JVM directly executes the bytecode without any intervention of the Operating System.
Operating systems generally don't have any sort of "here's some native code, please execute it for me" function; neither operating systems nor computers work that way. The native code that is generated essentially becomes a function in the JVM's address space, and the JVM simply invokes it.
Compiled code ready for execution is nothing but a bunch of bytes in memory, whether a JVM does it as it runs or a C++ or COBOL compiler does it ahead of time. Once it's in memory, the JVM can "branch" into that code and the CPU executes it. So there is no OS call, just a normal GOTO into its own memory space. The code eventually returns and the JVM figures out what to do next. It occurs to me this is similar to buffer-overrun attacks by hackers. They find ways to write bytes into memory that will later be executed, altering the program on the fly to do evil things.