2011-07-09

What is Bytecode in Java ?

BYTE CODE:

Bytecode is nothing but the intermediate representation of Java source code which is produced by the Java compiler by compiling that source code. This byte code is an machine independent code.It is not an completely a compiled code but it is an intermediate code somewhere in the middle which is later interpreted and executed by JVM.Bytecode is a machine code for JVM.But the machine code is platform specific whereas bytecode is platform independent that is the main difference between them.It is stored in .class file which is created after compiling the source code.Most developers although have never seen byte code (nor have ever wanted to see it!) One way to view the byte code is to compile your class and then open the .class file in a hex editor and translate the bytecodes by referring to the virtual machine specification. A much easier way is to utilize the command-line utility javap. The Java SDK from Sun includes the javap disassembler, that will convert the byte codes into human-readable mnemonics.




An example of bytecode is shown below
Let us take a sample program.A Main class is created with login function

 SOURCE CODE:
import java.io.*;
class Main
{
        public static void main(String args[])        
 {
  new Login();
 }
}


In command prompt compile it by typing "javac Main.java".
After that type "javap -c Main".You will get the byte code as follows

BYTE CODE:
Compiled from "Main.java"
class Main extends java.lang.Object{                   
Main();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V          
   4:   return

public static void main(java.lang.String[]);                                         
  Code:
   0:   new     #2; //class Login
   3:   dup
   4:   invokespecial   #3; //Method Login."":()V
   7:   pop
   8:   return

}



The "javap -c" command disassembles a class file.Disassembling done by Disassembler which converts machine language to assembly language.

IMPORTANCE:

The most knowledgeable C and C++ programmers know the assembler instruction set of the processor for which they are compiling. This knowledge is crucial when debugging and doing performance and memory usage tuning. Knowing the assembler instructions that are generated by the compiler for the source code you write, helps you know how you might code differently to achieve memory or performance goals. In addition, when tracking down a problem, it is often useful to use a debugger to disassemble the source code and step through the assembler code that is executing.


Comment for any doubts.......

Loading

Enter your Email here: