In this article, we’ll see the differences between Java Virtual Machine and Dalvik Virtual Machine
When I was learning Java programming seven years ago from now, I used to ask a lot of questions from myself and friends about this marvellous language but there was one question which popped up most of the time until I didn’t find the satisfactory answer. The question was why java bytecode can run on any machine or platform and how it is different from the other code?. The answer to this question was not easy for me at that time because I was just getting started.
So in this guide, I’m going to answer the following questions
A Java virtual machine ( JVM ) is an abstract layer between the Java program and the platform the Java code is running on. JVM is platform dependent and different implementations are available for specific platforms.
For example, If you have a
Hello.java class and when you run this class file then javac compiler turns your source code to
bytecode and creates
Hello.class file which means
javac compiler does not convert Java code directly to machine code like other compiler does. Bytecode is intermediate code which means neither humans can not understand this code and this code is not machine/platform dependent. Since bytecode is an intermediate code so you can give it to anyone to run it on any platform that’s why we call Java applications WORA (Write Once and Run Anywhere).
As you can see from the image above, once you have .class file ready then you can give this file to any platform and it will convert it to native machine code.
The Dalvik Virtual Machine (DVM) is a virtual machine which executes the android applications. Since everything in mobiles is very limited whether it would be a battery life, processing and memory etc. It had been optimised so that it can fit in with low-powered devices.
As you can see from the image above, everything is same as JVM except the last two steps. The Dex compiler converts the class files into the .dex file that run on the Dalvik VM. Multiple class files are converted into one dex file.
One of the main reasons of using DVM in android is because it follows the register based model and it is much faster than stack based model while JVM follows the stack based model which takes a lot of memory and also slower than DVM.
There are some major differences, so let’s have a look them
There are couple of reasons why Google not choose JVM over DVM, so let’s understand each one of them one by one.
JVM will work based on byte code and the DVM will work based on optimized bytecode, it is optimised for mobile platforms because mobile devices have less memory, low process and low power that’s why it is using the linux kernal.