Wednesday, March 22, 2017

What is JVM?

What is the JVM?
6 ANSWERS
Assil Ksiksi

Some background first. Any native software (say written in C or C++) you run on your computer is, in simplified terms, a list of very basic instructions that your computer’s processor (or CPU) can understand directly.

The best way to think of the JVM is that it’s avirtual CPU (or virtual machine) that sits between your Java program and your computer’s real CPU i.e. Intel, AMD, etc.

A Java program contains a list of instructions that only the Java Virtual Machine (JVM) will understand. If you provide these instructions to your CPU directly, it won’t understand them, and your program will obviously fail to run.

So what does the JVM do then? Well, it executes the instructions provided by your Java program, and converts them to instructions that your CPU can understand behind the scenes.

OK, so why have the JVM at all? Why not have the Java program consist of real CPU instructions so it can run faster?

The answer to that is simple. Your Java program can run on any operating system that can run the JVM, which means that you can compile your Java code once and run it anywhere!

As for speed, thanks to the thousands of man-years of effort put into the amazing piece of technology that is the JVM, it shouldn’t be too much of a concern unless you need every last bit of performance.

Abhishek Katiyar

JVM is

  • runtime engine to run java based applications
  • to run program, classes line by line
  • part of JRE, JRE is a part of JDK
  • responsible to load .class files

Let me give you a detailed overview about JVM and its working. Basically there are 3 major parts in JVM which are illustrated in figure below



JVM is divided into three major parts

Class loader sub-system: Is responsible to load .class file

Memory area: There are five memory areas

  • Method area
  • Heap area
  • Stack area
  • PC register
  • Runtime method Stacks

Execution engine

Method Interfaces

The methods which are not implemented in java instead they are implemented in some other languages are known as native methods ex: Clone methods, hash-code methods

Sometimes Java requires native methods libraries and Java Native interface is responsible to provide native libraries and Method Interfaces are responsible for providing such type of functionalities

Further more dividing Class loader sub system is divided into three categories :

  1. Loading
  2. Linking
    1. Verification
    2. Preparation
    3. Resolution
  3. Initialization

Loading :

  • Loading means reading .class file data and storing corresponding binary data in the method area of JVM
  • For each .class file JVM will store
  • Fully qualified name of class
  • Fully qualified name of immediate parent
  • Whether .class file represents class/interface/enum
  • Methods/Constructors/Variables information
  • Modifiers information
  • Constant pool information
  • After loading every .class file Immediately JVM will Creates an Object of the Type class Class to represent to represent Class Level Binary Information on the Heap Memory
  • Programmer can use Class Class object to access class information
  • .class class object will be loaded and created only once even though we are using multiple times in class class object.

Linking

(a) Verification

  • It is the process of ensuring that binary representation of a class is structurally correct or not
  • i.e. whether .class file is properly formatted or not
  • i.e. whether .class file is generated by valid compiler or not
  • Internally bytecode verifier is responsible for the following activities which is a part of class loader sub system
  • If verification fails then we get runtime exception saying java lang.VerifyError

(b) Preparation

  • In this phase, just default values will be assigned and original values will be assigned in the initialization phase

(c) Resolution

  • It is the process of replacing symbolic references from the type with direct references. It is done by searching into method area to locate the referenced entity.

Initialization

Initialization : In this phase, all static variables are assigned with their values defined in the code and static block(if any). This is executed from top to bottom in a class and from parent to child in class hierarchy.

Ashok Kumar
Ashok Kumar, Software Developer at Vistata IT


A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program. There are three notions of the JVM specification, implementation and instance. ... An instance of a JVM is an implementation running in a process that executes a computer program compiled into Java bytecode.

A Java virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets compiled binary code (called bytecode) for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions.Java was designed to allow application programs to be built that could be run on any platform without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the platform used java virtual machine is an abstract computing machine that enables a computer to run a Java program.

Singh Mani

A Java virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets compiled Java binary code (called bytecode) for a computer'sprocessor (or "hardware platform") so that it can perform a Java program's instructions. Java was designed to allow application programs to be built that could be run on any platform without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the platform.

Narasimha Prasanna HN

C and C++ programming languages when compiled forms an Object code which can be understood by the machine..u can see the files having extensions .o these object codes are not directly readable hence its difficult to write them. In order make the development easy we use Programming languages which are readable.

But the main disadvantage of object files is that they are some what Machine dependant. Machine independency is one of the main feature of Java beocz it uses JVM. Java Virtual Machine separates Programming land and Hardware land which allows Programmers to code without talking care of Hardware.. Whenever a Java program is compiled.. JVM generates a Byte Code or in simple terms a Class file. This Byte Code is understandable only by JVM.. JVM makes use of System Libraries and Hardware to run the program. The advantages of JVM is that it can be configured easily and Developers can test the project by modifying the memory resources used by JVM. Thus testing is easy becoz the developers will get to know how their application runs under different resources.

There are many types of JVMs available for different platforms ex . Dalvik VM which runs on Linux kernel powering Android Phones.

JVMs have eliminated the risk of developing a Java Based platform which can understand only bytecodes..beocz it is A Virtual Machine which can be used on any Platform with little or no Modifications..Thus Java is Portable.

2 comments: