Bytecode is one of the most common java programming terms which is used more frequently while learning java programming. Let's understand what bytecode means and how it makes java one of the most secured language.
Bytecode is a kind of language used by java to represent
.class files. Once you compile the java program,
the class file that gets generated contains the information in
the form of bytecode. So bytecode is an intermediate representation of your java program
in the form of
.class file. The word bytecode and
.class are used interchangeably.
At runtime Java virtual machine takes bytecode(
.class) as an input and convert this into machine specific code for further execution.
So java bytecode acts as machine code for java virtual machine.
Bytecode is also called as Magic code because
this along with JVM makes java platform independent and secure language. Making
any changes in
.class file will break down the class which means
you will not be able to run the program again. That is why it is
secure, doesn't allow any external editing. Java bytecode makes
java different from other languages like C, C++ where source code
is directly converted to machine code.
Who uses bytecode to run the program ?
It's JVM(Java virtual machine). Bytecode is basically input for JVM. JVM is responsible for loading and execution of java bytecode.
Does computer understands bytecode ?
No, Computer doesn't understand bytecode directly. At runtime the bytecode is converted into machine code specific to that machine by JVM interpreter. This machine code is used by the computer processor to execute and generate the output of program.
Is bytecode machine dependent code ?
No, Bytecode is machine independent code, it can be run on any machine. You just need JVM of corresponding machine's operating system to run the bytecode.
Who is responsible for generating bytecode ?
javac is responsible for compiling
the source code written in .java file and generate the
file written in bytecode.
Does bytecode means it's in the form of byte which is
No, Bytecode doesn't mean that it will be in bytes. It's just a way of representing information
Does same bytecode is generated of same program if compiled on different operating system ?
Yes, Same bytecode is generated of same program even if compiled on different operating system.
Write Once Run Anywhere(WORA) feature of java is
achieved with the help of bytecode. Programmer needs to write the
program once and can execute the
.class file generated from it anywhere and any number of times.
- The .class written in bytecode are usually smaller in size than source code, that means it also gets compressed. Therefore it is easy and fast to transport them over the network.
- As bytecode doesn't allow any external editing, it's also secure to transport them over the network.
- Though a .class file is not understandable, One can use
javap MyclassName.classto see some short information stored in .class file.
- Programmer should not focus much on understanding information stored in .class file.
- One more advantage of bytecode is, one can exchange java program without disclosing the logic implemented in it to others.