Core Java Tutorial 07 – What is a Java Virtual Machine ( why Java is a platform independent language )
In this video tutorial we will learn how Java works and what is a Java virtual Machine.
For our readers who are unable to access the Video Tutorial!!!
In this tutorial we will discuss HOW JAVA WORKS??? right from writing the Java Program till its execution…
When we write a Java Program using notepad or some other text editor like Eclipse IDE–the computer runs the program and gives us the desired output; but the computer does not understand anything written in Java Programming Language or any other language such as C, C++ etc;.So How does the Computer runs the program when it does not understands it???
It is the MICROPROCESSOR present inside our computer which runs the program but again it understands only one language called as MACHINE LANGUAGE and not any other programming language or a program written in Java.
People around the world use different types of Microprocessors in their computers for eg: microprocessors developed by Intel Company (eg x86 Based Intel Microprocessor ),AMD microprocessor developed by AMD Company etc.therefore different computers have different microprocessors inside them and all different microprocessors understands different machine languages. so there arises a need that in order to run a program on any particular computer written using any language, the program needs to be converted to that computer’s machine language.
A microprocessor is controlled by a big program called as OPERATING SYSTEM eg windows , Mac operating system , Linux etc .Whatever we see on the computer screen is the operating system and we instruct our operating system to perform a certain task and then the operating system in turn instructs the microprocessor to run the program needed to complete that task.
All computers have a program installed called as operating system in them. Any program which you want to run by a microprocessor is controlled by this big program i.e operating system. The operating system actually tells the microprocessor how to execute, what to execute, where to execute, to avail any resources needed for the program execution like input from keyboard displaying output of the program and many other tasks.
So let us go through the two important points we learnt so far :
1) Our program depends on a microprocessor of a computer because it ultimately executes a program. So, a programming language actually depends on a microprocessor.
2) Our program depends on an operating system of a computer because it controls the overall execution of a program by a microprocessor and helps us in availing all resources needed for the program to run.So, a programming language actually depends on an operating system too.
In this world, different people use different type of computers – meaning computers with different type of microprocessors and further different types of operating systems in them; so, when we write a program in any language, the challenge is – it should work on all these different computers with different combinations of microprocessors and operating systems- Therefore we need to invent a programming language such that if we write a program using it- it should be able to run on any computer.
Java does it beautifully- it says– you write a program once and run it anywhere i.e Java is a Platform Independent Language – a program written using java programming language can run on any type of computer which Java supports.
So How Java does it? for eg we have written a Java program now we pass this Java program as an input to another program called compiler provided by Java people and this compiler will convert this program into a language called as BYTECODE. This byte-code is given to all different computers and, all computers have a program installed on them called as JVM.
Java people provides JVM for almost all type of computers;this JVM on a particular computer understands the bytecode language, its microprocessor’s machine language and also contain all programs which are needed for it to establish communication between operating system program and bytecode.
JVM will convert the program written in bytecode language to the machine language and once the program is into its own machine language, the computer executes it.
So, this way: bytecode is executed by a JVM on all different type of computers.In this way the program written using a Java programming language runs on any computer i.e Java is a platform independent language and the reason why it is able to run on any computer is a JVM program.
Let us summarize what we just learnt : after writing a Java program we convert this program into a language which all JVMs understand. All computers will have their own JVM which understands the bytecode language and the machine language of that computer.When we give command to run the bytecode on any particular computer, JVM will convert the bytecode language to its computer understandable machine language and therefore runs it.
This is how Java works. This feature of Java to be able to run on any type of computer is one of the most famous reasons for Java’s popularity among other languages.