Articles

What is the meaning of bytecode in java?

What is the meaning of bytecode in java?

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  ()V Code
  	   LineNumberTable LocalVariableTable this Lbasic/HelloJava; main ([Ljava/lang/String;)V	 
   java/lang/System   out Ljava/io/PrintStream;  Hello World
    java/io/PrintStream   println (Ljava/lang/String;)V  
Hello Java ! Hello Learning args [Ljava/lang/String; 
SourceFile HelloJava.java !               /     *· ±    
                 
   	       O     ² ¶ ² ¶ ²  ¶ ±    
             
         " #    $    %

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:


Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com
Page Views : 194    Published Date :Feb 22,2020   Author :rajneesh
Please Share this page

Popular Articles

Loading...
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!