Why Should I Care About the JVM?
Possibly the cleverest thing that Sun ever did was create the JVM that underpins Java. JVM is short for Java Virtual Machine and it comes as quite a surprise to many Java developers to find out that the most commonly run JVM (Oracle's HotSpot) is actually written in C++. So what is a JVM and why is it needed? Portability for one, but also it allows Java code to be executed in a virtual environment rather than directly on the underlying hardware. Sun didn't originate this technique -- it was used in mainframes in the 1960s and on Apple IIs and later computers running the USCD Pascal system. Writing portable software has always been problematic because the different processors have different instruction sets. Processors have varying word sizes, different endianness and some processor architectures use registers instead of stacks.The C language was invented to be a portable high level assembler but moving a large program between different processors and operating systems was still not that much easier. Once the JVM is written for a processor, Java programs can run on it without having to worry about the portability considerations. Running in a virtual machine also offers other benefits, such as safeness. It's not difficult to write a program in C that crashes through trying to access memory addresses that it shouldn't. It's even possible to crash a computer and need a reboot with C. Java programs cannot do that; they are compiled into bytecode, an intermediate language. When a Java program is loaded by the JVM, the bytecode is verified so nothing untoward happens. Instruction decision branches are guaranteed to jump to valid locations, data is properly initialized and references are always type-safe. Plus access to private or package private data and methods is tightly controlled. The compiled bytecode is stored in .class or .jar files. These get distributed and will work on any environment where the JRE (Java Runtime Environment) exists. The JRE includes the JVM plus a collection of Class libraries that are distributed in bytecode. Having loaded the bytecode into memory, the program can now run. The older method of doing this is interpreting, which is how the GNU Compiler for Java works. It calls code to interpret each bytecode and evaluate expressions. To get better performance, the bytecode is compiled into native machine code. This is called Just-In-Time (JIT) compilation as it's only done as the code is required. There's a tradeoff between the degree of optimization the JVM can achieve and how long it takes to start execution of the code. It's just not acceptable to take 10 minutes optimizing the code before running it. Some JVM's, like Oracle's HotSpot, monitor performance and can improve execution speed through targeted optimization, better it's said in some cases than even hand-coded C or C++.