wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What happens to the bytecode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What happens to the bytecode" Watch "What happens to the bytecode" New topic
Author

What happens to the bytecode

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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.


Thanks,
Shekar
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.
 
Consider Paul's rocket mass heater.
 
subject: What happens to the bytecode