Scanner class Methods in Java

JVM(Java Virtual Machine) & Bytecode

What is JVM?

 In the previous post we learned about JDK. We have read JVM a lot of times in previous posts. Now, lets understand what a JVM is?

 A JVM (Java Virtual Machine) is a hypothetical computer platform designed to act as computer that does not really exists as a hardware. Instead its an emulator program that sets aside a part of  your hard drive to act like a virtual computer (JVM) which  executes Java programs

JVM is responsible for making Java a platform independent language as it follows, "Write-once-execute-anywhere'' approach. This feature is achieved as the Java Compiler does not translate language in machine code, instead to bytecode. Bytecode is not understood by underlying operating system. So, an application is needed to convert that bytecode into machine code and this is achieved using JVM.
JVM converts bytecode into machine code.


The diagram shows , how a program file converted into machine code in all types of operating systems. 
A java source file goes to java compiler which converts it in bytecode by changing  it  a .class file from .java file then the JVM for that OS, convert it in machine code or binary which is understandable by computer.

Because of JVM you can execute a Java program on any computer has a java runtime environment installed without recompiling the program.This is how java became a platform independent language.

Bytecode:- 

In java ,source code is not compiled into machine but rather special intermediate format Bytecode. Java bytecode is a form of  instruction that the JVM reads and translates them one by one into machine code which computer understands.When JVM loads class ,it gets a stream of bytecode for each method in class. The bytecodes for that method will be executed, when the method is invoked during  the implementation of program.

How bytecode represents instructions of java? 

Each instruction in method's bytecode stream consists of an one-byte opcode specifying the operation to be performed followed by zero or more operands embodying values to be operated on.Since an opcode is one byte in length so it can take 256 possible opcodes. Some of them are reserved or not used by language.

Example of Bytecode:- 1) int is represented as iload which means an int value is being loading. 
                                      2)The instruction i2f is used for the conversion from int to float.

Command to see bytecode of a program

You can check bytecode of any java program file using following command in Command Prompt:-

                                              javap -c filename.java

Example:- Lets create a java program Main.java and check its bytecode using above command.
 
Program to print Hello world in java


It is a simple program printing Hello World! on screen with integer value 10.
                                      
use of javap -c command to check bytecode of a java program

Experiment:-  Create your own java program and check their bytecode. Happy Coding!

Comments