What is JVM (Java Virtual Machine): Most programming languages such as C/C++ compile source code directly into machine code suitable for execution on a particular microprocessor architecture or operating systems such as Windows or Unix. This property does not make these languages’ architecturally neutral’. However, Java is an architectural neutral language as it follows. Write once execute anywhere approach. This feature of Java achieved as the Java compiler does not translate the source code into the machine language of the computer that the program is running on. Instead, the compiler translates source code into bytecode which is not understood by the underlying operating system. So an application is needed that can convert this bytecode into machine code understood by the underlying operating system. It is accomplished using the Java Virtual Machine.
You can think of the Java Virtual Machine (JVM) as an open computer platform – a design for a computer that does not exist as actual hardware. Instead, it is an emulator, i.e., a program that sets aside a part of your hard drive to act like a computer (namely the JVM) that can execute Java programs. Because of JVM, you can execute a Java program on any computer that has a Java runtime environment installed without recompiling the program. It is how Java provides platform independence. It is the Virtual Machine that does all the code processing, and because Oracle owns Java, you must have the Virtual Machine or Java Runtime Environment.
Java is both compiled and an interpreted language. When you start writing your Java code, you do it in a text editor, and NetBeans is all set up to provide you with the place to write it all. The code is known as source code and is always saved with a file extension of .java. A program called Javac will then turn that source code into Java Byte Code in a process that is known as compiling. First, the java compiler translates source code into the byte code instructions. Once the code has been compiled by Javac, it gets saved again, this tie with a file extension of .class. In the next stage, the java interpreter converts the byte code instructions to machine code. However, this can only be done if the code is free of errors. When the class file is ready, that can be directly executed by the machine running the JVM (Java Virtual Machine). The intermediate code namely the bytecode produced by the java compiler is for a machine that exists only on the memory of a computer. So:
● Your source code is created and saved with the .java extension.
● Javac then compiles the code and saves it with the .class extension.
● Your compiled class can be run on the JVM NetBeans is designed to make your life very easy. It does all the creating and compiling for you; behind the scenes, it is working the magic of transforming source code to java files, using Javac to compile and then running your new .class file in its built-in software. It saves you having to open terminal windows and type in long, complicated strings of text, as you do with other programming languages. Okay, so that is how NetBeans works with Java, time to open NetBeans – we are going to write a program. Writing code is the best way of learning how it all works so go ahead and do what you need to – if you haven’t installed NetBeans yet go and do it. Then open it and get ready to follow along with the rest of this section.
This machine or the ‘Simulated computer within the computer’ is known as the “Java Virtual Machine” or JVM.
The JVM can be thought as a mini operating system that forms a layer of abstraction where the underlying hardware. The operating system and the compiled code is concerned the compiler converts the source code into a code that is based on the imaginary computer instructions set. The compiled code is not processor specific.
The interpreter converts these instructions for the underlying hardware to which the interpreter is targeted. The portability feature of the .class file helps in the execution of the prg on any computer with the java virtual machine. This helps in implementing the “write once and run any where” feature of java.
We’ll be covering the following topics in this tutorial:
Lifetime of a JVM
The runtime instance of the java virtual machine has a clear mission in life. To run one java application. When a java application starts a run time instance is ban when the application completes the instance dies. If you start three java applications at the same time on the same computer using the same can create and implementation.
You will get three JVM instances. Each java application runs inside its own java virtual machine. A java virtual machine instance starts running its solitary application by invoking the main() method of some initial class. The main() method must be public, static, return void and accept one parameter: a string array. Any class with such a main() method can be used as the starting point for a java application.
JVM Architecture
ClassLoader: It is the sub-system of JVM. When the compiler successfully compiles the java source code, then it generates java byte codes Java bytecodes are residing in the .class file. Internally .class file resides in the current working directory. Class loader subsystem takes the copy of the class file and loads it in the memory before JVM executes java bytecodes.
It Loads the .class file into memory and checks the correctness of the .class file. It allocates the memory to the static block and static variable. If the static variable is not initialized it assigned the default value to the static variable and the static the variable initialized when the class file loaded in the memory.
Method Area: It is just a logical memory component of JVM. It holds information about the class’ and interface. All the static variable, static block, static method resides in the method area. Size of method area not fixed because it’s size depends on the size of the application.
Heap Area: Heap is another logical memory component used by JVM. When the program constructs the object through the memory from the heap.
Java Stacks: It is another logical component used by JVM. Java language Stacks are used by the virtual machine to store local variables, and it’s partial results. Each thread creates its own JVM stack. This area has create one stack frame for each thread. Once the thread execution completed, this stack frame also gets deleted.
PC Registers: It is popularly known as the program counter register. PC register holds the address of the JVM instruction which is currently executing. In Java, each thread has its PC register to hold the address of the currently executing instruction.
Native method Area: Native method stacks hold the instruction of native method information on the native library — it writes in a different language instead of Java. In java, Every thread has a separate native method stack.
Execution Engine: It treated as a virtual processor, and it generates, manages & executes java bytecodes. It is a type of software used to test hardware, software, or complete systems. The test execution engine never carries any information about the tested product.
Native Method interface: The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to call by libraries and native applications.
Native Method Libraries: It is a collection of the Native Libraries(C, C++) which are needed by for the Execution Engine.