Demystifying JAVA's Bytecode.

Demystifying JAVA's Bytecode.

Java had always been known for its security, flexibility and portability.

Java changed the way the world looks at the issue of portability and security.

A program, when downloaded might contain any virus which may possess a threat to the system. For instance, a virus program might collect some private information such as bank account balances, and passwords, by searching the content of your local computer’s file system. Then comes, portability, it’s not practical to have different codes for varying environments, and for different operating systems. So, to overcome this tedious task JAVA comes up with Bytecode, which solves both security and portability problems. So, in this blog, we will take a closer look at what JAVA bytecode is, how it works and why it’s such an important aspect of the JAVA ecosystem.

What is Bytecode?

Bytecode is a highly set of optimized set of instructions designed to be executed by the JAVA runtime system, which is called the JAVA Virtual Machine. JVM is an abstract machine that provides the runtime environment in which java bytecode can be executed. Although the specifications of JVM differ from platform to platform, all understand the same bytecode, which makes the Bytecode platform independent. This provides the Write Once, Run Anywhere capability for which Java has been famous.

How does bytecode works?

  • The process starts with the creation of Java source code.

  • The source code is then compiled into a Java bytecode file using the javac compiler.

  • The Java bytecode file is then loaded and decoded by the Java Virtual Machine (JVM).

  • The JVM executes the bytecode, which is then translated into machine code that can be run on the host computer.

    How can you display the Bytecode?

    Assuming the following java source file:

      class Test{
      public static void main(String args[]){
          System.out.println("Hello World!!");
          }
      }
    

After compiling the source file, open a terminal and navigate to the directory containing the class file that you want to display the bytecode for.

Now run the following command to see the actual bytecode of a class file.

javap -c -p -v [path to the .class file]

  • -c is used to disassemble the JAVA class.

  • -p is used to expose the private members of the class

  • -v is used to view verbose information like stack size and constant pool.

    If we perform javap command without verbose, the result looks like this:

If we add a '-c' '-p' option to the command so that, we will see the Java bytecode for the methods in the class.

In conclusion, Java bytecode is a crucial aspect of the Java ecosystem. It allows Java programs to be run on any device with a JVM, provides security benefits, and can improve performance. Next time you run a Java program, take a moment to appreciate the power of bytecode at work!

I hope this blog had helped you to learn about JAVA's bytecode. In case, you wanna know me more, here it goes. I'm an undergraduate making myself learn frontend development, data structures and algorithm and a lot more. Apart from that, I'm a content writer at HackForCode. I am recently embarking my journey on 100 days of code challenge.

You can connect with me on: Twitter

Happy Learning!!!