top of page

What is Byte Code? | How does it works in Java? | Brain Corner

Updated: Sep 19, 2021


ree

'Byte Code'

Do you know that computer programs are very closely tied to the specific hardware and operating system they run on. For example, a Windows program will not not be able to run on a computer that only runs DOS; A Mac application can't run on a Unix Workstation, and so on. To tackle with diversity of platforms, major commercial applications like MS-Word or Netscape Navigator have to be witten almost independently for all different platforms they run on.


But for the applications developed with Java, this is not the case. For, these applications are platform independent i.e. they are not affected with changing platforms. Java solves the problem of platform-independence by using byte code. In order to understand the byte code, you must be clear about compilation process first, which is being discussed below.


'Ordinary Compilation Process'

As you know that a program is a set of instructions given to a computer. The program or code written by a programmer is usually called the Source Code. This source code needs to be converted into machine language code, which a computer can easily understand. The process of converting a source code into machine code, is called compilation. The converted machine code depends a lot on the platform it is executing upon. That means for different platforms different machine code is produced. This resultant machine code is called native executable code.


There is an alternative to compiling a high-level language program. Instead of using a compiler, which translates the program all at once, you can use an interpreter, which translates it instruction-by-instruction, as necessary. In order to execute a program, the interpreter repeatedly reads one instruction from the program, decides what is necessary to carry out that instruction, and then performs the appropriate machine-language commands to do so.


'Java Compilation'

Contorary to ordinary compilers, the Java compiler does not produce native executable code for a particular machine. Insteads it produces a special format called byte code.


The Java byte code looks a lot like machine language, but unlike machine language Java Byte Code is exactly the same on every platform. This byte code means the same thing on a Solaris workstation as it does on a Mackintosh Power Book or on Windows 98 running on an Intel Pentium IV. However, the Java programs that have been compiled into byte code still needs to an interpreter to execute them on any given platform. The interpreter reads the byte code and translates it into the native language of the host machine on the fly. Since the byte code is completely platform independent, only the interpreter and a few native libraries need to be ported to get Java to run on a new computer or operating system.


'Java Virtual Machine'

As you are aware that any source program needs to be either compiled or interpreted before it can be executed. But with Java a combination of these two is used.


Programs written in Java are compiled into Java byte code, which is then interpreted by a special interpreter for a specific platform. Actually this Java Interpreter is known as Java Virtual Machine (JVM). The machine language for Java Virtual Machine is called Java byte code. Actually the Java nterpreter running on any machine appears and behaves like a "virtual" processor chip, that is why, the name - Java Virtual Machine.

JVM combined with Java APIs makes Java platform. The Java API (Application Programming Interface) are libraries of compiled code that can be used in your programs. In other words, the Java API consists of the functions and variables that programmers are allowed to use in their applications.

We can now summarise the working of Java. Firstly a Java program is compiled in the form of byte code. Then Java Virtual Machine (JVM) interprets the byte code for the machine on which it has to run.


ree

I hope you would have liked this post, If you want more posts like this then you can be a member of this comunity and can share your support to me.


For becoming a member, click below. It's free!!

ree


Comments


Post: Blog2_Post

Subscribe Form

Thanks for submitting!

9007786621

©2021 by Brain Corner. Proudly created with Wix.com

bottom of page