What is JVM?
Java Virtual Machine is a machine language architecture developed by the Sun Micro systems to compile the programs written using Java. JVM is introduced as a cross-platform computing program.
There are several components used in JVM:
It is the storage area for methods and variables. It can be changed according to the registers.
Stack can be changed using different different registers like optop, vars, frame and program counter. Vars register are local variables while frame register point towards the execution of the stack . The optop register are used to execute the Java bytecode instructions. Program counter is used to indicate the method area of bytecode.
Java bytecode is stored in the method and it enables easier synchronization of threads.
This component of JVM actually stores all the Java objects. Stack stores only the references of Java objects while the real objects are stored only in Garbage collector. Garbage collectors are used to free memory which were allocated other objects.
What is Java bytecode?
Machine languages are simple instruction which can be executed directly into the machine processor. All the java programs written in high level languages require an translating program which translates those high level language into machine language. Usually these processes are done by a program called compiler but every machine has an unique compiler which does not execute the programs written in other machines. So interpreter programs are used to translate the every line of the high level language and feed the translated code into the compiler. The programs written traditional compiling process cannot be used in all the computers so Java developers developed a new process which is a combination of compiling and interpreting.
When Java was new programming language, many users criticized it for being too slow. The processing of Java was slow because the compiler took ages to understand the complex high level language and process it. This problem also forced the developers to develop an interpreter which is used exclusively for Java; this interpreter is called as Java bytecode.
Programs written in Java can be compiled using JVM. Since the machine language for a computer does not exist JVM uses Java bytecode. The Java bytecode acts like an interpreter and translates the high level Java program into machine language. Java bytecode cannot be used as a machine language because it only creates a code which can be used in all the machines. A dedicated Java bytecode interpreter is necessary for all the machines to compile a Java program. Only if a computer has a dedicated bytecode interpreter it can translate the programs from other machines.
Why a Java bytecode is preferred over Java compiler?
Compiler is a very complex program on the other hand Java bytecode program is relatively simple which makes writing a bytecode interpreter easy than writing a complex compiler program. If you still prefer to write a compiler program imagine yourself writing a compiler program for every machine in which you want to run a compiled Java program.
Is Java bytecode is operating system independent?
Since JVM uses Java bytecode only for a specific compiler all the Java byte codes are not compatible with every OS. The compiling processes differ between each operating system so a separate Java bytecode is required to every operating system. The Java bytecode translates the language according to the corresponding OS used. Java earned the tag of portable language because of the portability of programs written but not because of the portability of the JVM between the OS. However Java bytecode can be used between two machines using the same OS.
Despite the fact that the JVM cannot be used between two operating systems; the Java bytecode can be shared between the computers using same operating system. This facility can be made available by selecting the platform from the list displayed during the installation of JVM.
Almost every Java programs are downloaded through internet which may raise several security problems. If you are running the programs using Java bytecode, it runs the program indirectly which protects your system from dangerous programs.