Articles

What is the meaning of bytecode in java?

What is the meaning of bytecode in java?

In this article, our focus will be on understanding:

  • What is bytecode?
  • How is bytecode helpful in making java platform independent?
  • An example demonstrating creation of java bytecode for better understanding of concept.

Bytecode plays a vital role in java programming laguage.

Here we will learn about bytecode, see its definition and understand why bytecode is needed?

 

What is bytecode in java?

Bytecode is a specialized set of instructions that is read, verified and understood by JVM or Java Virtual Machine.

 

When a java source code is compiled then it is converted into bytecode which could be executed on same system or ported to other system having JVM and executed.

Bytecode and JVM together helps in making java platform independent.

See the below diagram to understand the above statement:

java program compilation

 

If you go to the java classpath, where your .class files are stored and try to open the file you can see the generated bytecode for your java code. Even though you will not be able to understand anything that is written in the .class file but you will get the feel of how bytecode present in .class file looks like. But remember, JVM will easily understand what is written in .class file.

 

Bytecode helps in following ways:

  • Since java code is converted to bytecode which can be easily interpreted by any system that has JVM, so it helps in making java portable programming language.
  • Bytecode and JVM together helps make java - platform independent, a very popular feature of java program.

Lets see a practical view for above example to understand creation of .class file and bytecode in java:

 

We are going to write a simple java program with file name - Hello.java

Source: Hello.java

package basic;

public class HelloJava {

	public static void main(String[] args) {
		System.out.println("Hello World");
		System.out.println("Hello Java");
		System.out.println("Hello Learning");

	}

}

Compile the above code. I have compiled and run it from Eclipse.

To compile and run from command prompt, use below commands:

  • Compile: javac HelloJava.java
  • Run: java HelloJava

A Hello.class file will be created at the classpath similar to the one shown in below image. 

java .class files containing bytecode

 

If you open the above HelloJava.class file with the help of notepad, you will see the bytecodes as shown below:

 

Bytecode in Hello.class file

Given below is the content of the Hello.class file.

Êþº¾   4 &  basic/HelloJava  java/lang/Object 

Above content can be read, verified and interpreted by JVM present in the system.

 

This is all about bytecode in java.


You can visit given links for:


Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Rohanjit Kumar
Technology geek, loves to write and share knowledge with the world. Having 9+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Jun 28,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!