How Java code works?

In the previous post, we learned how to print hello world using Java. And we used two commands in terminal – javac and java (to compile and run java program).

These two commands are required to achieve platform independence (meaning you can run this compiled java program on any operating system). You compile your human readable code using javac into a bytecode file. This is essentially an object file which will be used by Java interpreter (java command).

When you install JDK, you also install JVM (Java Virtual Machine) along with it. This is where the magic happens! JVM runs on top of your OS. So the compiled code is never really executed by your OS and is always executed by JVM.

This is why you can simply move your compiled object file (bytecode) to a different operating system and still manage to run your java program.

If you need more detailed information on this, you can refer this awesome page and let me know what you think in the comments below!