What is JVM and byte code?


We all know that the code will be written in Java is compiled by the java compiler into byte code. These are the .class files that we create that may be packaged together into a jar or a war file. when we run our application using the Java command line, the byte code is then run by the Java Virtual Machine or JVM.

We often think of this as the JVM or the virtual machine is interpreting the byte code. This structure compiling our raw Java code into byte code and then the byte code is interpreted at runtime is how Java achieves one of its benefits which is called write once run anywhere.

When Java was first created this was an important feature because of that we can write code once and run it with consistent results on any hardware that has a Java Virtual Machine. So this same Java code can be run on a Mac, Windows, Linux, or any other operating system on which a JVM exists. And that's because our Java code is going to be compiled into byte code and the JVM the Java Virtual Machine is able to interpret this byte code.

But the Java virtual machine, however, is not simply interpreting byte code. it contains a number of features and complex algorithms to make it more efficient than a traditional code interpreter would be.

If you were writing code in a language such as PHP which is not compiled but is interpreted at runtime using an interpreter such as the Apache webserver.

php code interpreter


Well, each line of P.H.P code is only looked at analyzed, and the way to execute it determined as it is needed. But within the Java Virtual Machine, It's a much more complicated process. So the job of a virtual machine is not being asked to run Java code but rather Java byte code and in fact, any language like groovy, scala which can be compiled to JVM compatible byte code can be run on the Java Virtual Machine.

Now we're going to focus on what happens when we ask the JVM to actually run some byte code.