Difference Between Source Code and Byte Code


When we start learning Java, we often come across the two terms Source code and Byte code. When a programmer writes Java code, it is in a form that a machine cannot understand. This code is termed as source code, which is easy to read and modify by humans, but not by machines. Therefore, before execution of code, we need to convert it into a machine readable format that is termed as machine code. However, Java first converts the source code to an intermediate code called byte code and then, into a machine code. Let’s discuss the difference between the source code and byte code in Java.

Source Code vs Byte Code

Source Code

It is the original code that a programmer writes using a programming language, such as Java, Python, C++ and so forth. Source code contains instructions, statements, variables, comments, and other elements that make up a full fledge program. This code is easily readable and modifiable by humans, but it is not directly understandable by computers. The extension for source code of Java programming language is ‘.java’.

Here, is an example of Java Sorce Code −

Example

public class Example {
   public static void main(String[] args) {
      System.out.println("Tutorialspoint Welcomes you!!");
   }
}

Output

Tutorialspoint Welcomes you!!

Byte Code

It is an intermediate code that is generated after the successful compilation of the source code. This code is not written by humans, but by the Javac compiler that exists inside the Java Virtual Machine. It is an intermediate-level code that consists of binary, hexadecimal, or macro instructions. We need JVM because byte code is not directly executable by computers either, but it can be processed by this virtual machine, which converts it into machine code.

The Java programming language is platform independent because of this portable byte code. Once the Java source code gets compiled by the Javac compiler, it can run on any system that has a Java virtual machine installed.

The following snapshot represents the byte code of the previous example program generated by Javac Compiler:

The extension for byte code of Java programming language is ‘.class’.

Difference between Source Code and Byte Code

The following table concludes the difference between Source code and byte code from the above discussion −

Source Code

Byte Code

It is written by the programmers.

It is generated by the Javac compiler

A human can read and understand the code.

It can be read and understood by the Java virtual machine.

It is a set of instructions written in human readable format using a programming language.

It is a set of instructions written in machine readable format using a virtual machine

Source code needs to be converted into byte code before execution

Byte code needs to be converted into machine code before execution

It is a high level language.

It is an intermediate language between source code and machine code.

Source code can contain instructions, statements, variables, and comments.

Byte code can contain binary, hexadecimal, or macro instructions.

Conclusion

In Java, the source code and byte code are different levels of representation of a set of instructions for the machines. The Java bytecode is an intermediate representation of Java source code that is compiled by the Javac compiler, whereas source code is an original code written in high level language by the programmers.

Updated on: 21-Jul-2023

265 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements