The JVM optimizes bytecode by creating something called a code cache . Unlike C ++, the JVM can collect a lot of data about your program, for example, "How hot is this for the loop?". Is this block of code even worth optimizing ?, etc. Therefore, optimization here is very useful and often gives better results.
If you optimize when translating from java to bytecode (i.e. when calling javac), your code may be optimal for your computer, but not for any other platform . Therefore, it makes no sense to optimize here.
As an example , suppose your program uses AES encryption. Modern processors have a set of instructions based on AES, with special equipment to make encryption much faster.
If javac tries to optimize at compile time, it will either
- Optimize the instructions at the software level, in which case you will take advantage of modern processors or
- replace your AES instructions with equivalent CPU-AES instructions supported only on new processors, which will reduce your compatibility.
If instead javac leaves them as they are in byte code, a JVM running on newer processors can recognize them as AES and use this CPU feature, while JVM running on older processors can optimize them at the software level in runtime (code cache), providing you with both optimality and compatibility .
source share