How Java works?

Haneef Muhammad
2 min readJun 12, 2021

Ever wonder how Java works, here is an insight into how java works from the knowledge I acquired from different resources.

So you write a code in java in a human readable format, this code is not readable by your computer as it does not know the language in which we have written the code.
So we need someone who can translate the language which we have written the code to the one the computer understands(Machine Language). In comes compilers, who help solve our problem.

In a language like C, what happens is, the human readable code is converted to machine code by the compiler and this machine code is run by the computer. This same methodology will be followed by different programs with some modifications.
Now before getting into how java compiles we need to know that the code compiled by the C compiler i.e, the machine code cannot be run in a different OS as it is OS dependent. This dependency problem will not be present in Java.
Cool now let us look at how java does the same job with the help of the below diagram,

Working of Java.

So the code written by us is the Source code, Java has a compiler called javac which converts the high-level language or the source code to an intermediate language called bytecode. And this bytecode will be in .class format.

So the source code in human readable format with file extension as .java is converted to byte-code with file format in .class. Now this is converted to machine understandable format by the JVM. So one advantage of this is that, it makes java OS independent. So the JVM will be OS dependent but the .class file produced by your compiler can be run on different OS using the JVM, whereas in languages like C, the compiled .exe file cannot be run on different OS.

--

--