The Sun HotSpot VM -which is used since Java 1.3 if memory serves- takes measurements of which methods and classes are heavily/lightly used at runtime. Based on that it may actually decide NOT to compile a method, and to interpret it every time it gets called. It definitely does not compile everything at once at startup. Other JVMs (IBM, Kaffe, SableVM...) may work differently.
hi , i have just started learning java. what exactly happens when a java program is complied ? doest it make a Obj file ? and what happens when u run a java program , ? does it form a EXE file like in C or c++ ?
thanks in advance
Joined: Aug 27, 2005
No its not precompiled, it is compiled during runtime by the virtual machine.
Which brings up a question of my own: I heard that Sun was working on this "just in time" thing where the VM would compile the entire code at once so it would run faster. Has anything been done with that?
Joined: Oct 20, 2005
Hi William, Here are some links to answer your question and to help you learn more:
Java is pre-compiled. However, it is not pre-linked. When you create your .class files from your .java files, that is essentially the same as creating .o (or .obj) files from your .c/.cxx files. (There are differences, of course -- most notably a single .java file can produce multiple .class files -- but we won't get into those differences).
The biggest difference is what Java get compiled into. You do not compile to platform-specific machine languages. Instead, you compile to Java bytecode. The Java bytecode (the .class file) is identical on every platform because it is run through the Java Virtual Machine (JVM). You could think of the JVM as an interpreter for the bytecode, thus making Java both a compiled and an interpreted language. You get better performance than a standard interpreter, however, because you are interpreting things that are already parsed and (presumably) optimized by the compiler. You don't have to re-parse the human-readable code each time.
However, as pointed out above, even this was not necessarily fast enough for various applications. So Just In Time compiling was introduced. A platform that has a JIT compiler will produce machine-code for that platform at run time. As noted above, this compiling is optimized and is entirely beyond your control (unless you write your own JVM with JIT compiling...), so the usual recommendation is to not worry about it. Knowing about compiling to .class files and about what bytecode is and its functionality is more important than knowing about the JIT compiler.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.