Have you every wondered when you program with Java, why do you always have to compile your code? What does the ".class" file mean? Well, the answer is that your computer does not read English words, and the compiler takes your code and translate into machine-level code. This process is more complicated for Java. Your code is first translated into bytecode and then executed by JVM (Java Virtual Machine), and finally processed by the computer, as shown in the picture above.
Bytecode
Bytecode is the output from a Java compiler and is a form of instruction set designed for efficient execution by a software interpreter. It is composed of numeric codes, constants, and references (normally numeric addresses). Each bytecode is has one, or in some cases two bytes that represent the instruction (opcode), along with zero or more bytes for passing parameters. Below is an example of the bytecode:
Java Virtual Machine
JVM (Java Virtual Machine) is a virtual machine that executes bytecode outputted by Java compiler and enabled the computer to run the program. Java is powerful, because once a program is compiled, it can be run on any platform without having to be rewritten or recompiled. A Java virtual machine can interpret the bytecode one instruction at a time or interpret the bytecode with just-in-time compiler.
Machine code:
While humans understand languages, computer's CPU (central processing unit) only understand machine language, aka machine code, which is the most elemental language of computers, comprising a long sequence of zeros and ones. Each machine code gives CPU an instruction to perform a simple operation or to store data in RAM.
--------------------------------------------------------------------------------------------------------------------------
Picture References:1.https://www.google.com/search?q=java+virtual+machine&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiW_ZSk5trPAhUGVT4KHd2UBfgQ_AUICygE&biw=1280&bih=628#imgrc=mRJiumYp6_53fM%3A
2.https://en.wikipedia.org/wiki/Java_virtual_machine#/media/File:JvmSpec7.png
3.https://en.wikipedia.org/wiki/Java_bytecode
Writing References:
1. http://durofy.com/10-major-differences-between-c-and-java/
2. https://en.wikipedia.org/wiki/Java_virtual_machine
3. https://en.wikipedia.org/wiki/Machine_code
4. https://en.wikipedia.org/wiki/Java_bytecode
5. http://www.theserverside.com/definition/Java-virtual-machine-JVM
--------------------------------------------------------------------------------------------------------------------------



I'm really glad that you wrote this article! I was always very confused about what the .class files were and if I needed them and what the overall point of them was. It is interesting to learn that the file is a translation between humans and the computers, because as I wrote about how translators work on a previous post, languages are converted into english because that is the easiest to compare and for computers, the .class is the easiest to compare. Great article!
ReplyDeleteWow that really helps me understand the reason why we have to compile our programs first before we run them. I always knew that what we typed into java was not directly what the computer read but I never knew this was where the .class files came into play. Thanks for the great post that really helped a lot!
ReplyDeleteAfter half a semester long of doing Java it's great to read about the Java code and it's virtual machine and how all our codes have been compiled and ran in the console.Thank you for such a great article !
ReplyDelete