Java JVM

Q. What is Java?
Java is a programming language and a platform.
Platform: Any hardware or software environment in which a program runs, is known as a platform. 
Since Java has a runtime environment (JRE) and API, it is called a Platform.
Java is a high level, robust, object-oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995
Java was developed by James Gosling, who is known as the father of Java, in 1995.

Q: What are the top Java Features?
Simple: Java is quite simple to understand and the syntax
Platform Independent: Java is platform independent means we can run the same program in any software and hardware and will get the same result.
Interpreted: Java is interpreted as well as a compiler-based language. 
Robust: features like Garbage collection, exception handling, etc that make the language robust.
Object-Oriented: Java is an object-oriented language that supports the concepts of class,  objects, four pillars of OOPS, etc. 
Secured: As we can directly share an application with the user without sharing the actual program makes Java a secure language. 
High Performance:  faster than other traditional interpreted programming languages.
Dynamic: supports dynamic loading of classes and interfaces.
Distributed: feature of Java makes us able to access files by calling the methods from any machine connected.
Multithreaded: deal with multiple tasks at once by defining multiple threads
Architecture Neutral: it is not dependent on the architecture.

Q: What is JVM?
JVM (Java Virtual Machine) is an abstract machine. 
It is called a virtual machine. 
It is a specification that provides a runtime environment in which Java bytecode can be executed. 
It can also run those programs which are written in other languages and compiled to Java bytecode.
Java applications are called WORA (Write Once Run Anywhere).
JVM (Java Virtual Machine) runs Java applications as a run-time engine. 
JVM is the one that calls the main method present in a Java code. 
JVM is a part of JRE (Java Runtime Environment). 
When we compile a .java file, .class files (containing byte-code) with the same class names present in the .java file are generated by the Java compiler. 
This .class file goes through various steps when we run it. 
These steps together describe the whole JVM.

Core Components Of JVM
1. Class Loader Subsystem
  a. Loading - The Class loader reads the “.class” file, generate the corresponding binary data and save it in the method area. After loading the “.class” file, JVM creates an object of type Class to represent this file in the heap memory. 
  b. Linking - Performs verification, preparation, and (optionally) resolution.
    verification: It ensures the correctness of the .class file i.e. it checks whether this file is properly formatted and generated by a valid compiler or not. If verification fails, we get run-time exception java.lang.VerifyError. This activity is done by the component ByteCodeVerifier. Once this activity is completed then the class file is ready for compilation.
  preparation:  JVM allocates memory for class static variables and initializing the memory to default values. 
  resolution: It is the process of replacing symbolic references from the type with direct references. It is done by searching into the method area to locate the referenced entity.
  c. 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 the class hierarchy. In general, there are three class loaders:  
    Bootstrap class loader: Every JVM implementation must have a bootstrap class loader, capable of loading trusted classes. It loads core java API classes present in the “JAVA_HOME/lib” directory. This path is popularly known as the bootstrap path. It is implemented in native languages like C, C++.
    Extension class loader: It is a child of the bootstrap class loader. It loads the classes present in the extensions directories “JAVA_HOME/jre/lib/ext”(Extension path) or any other directory specified by the java.ext.dirs system property. It is implemented in java by the sun.misc.Launcher$ExtClassLoader class.
    System/Application class loader: It is a child of the extension class loader. It is responsible to load classes from the application classpath. It internally uses Environment Variable which mapped to java.class.path. It is also implemented in Java by the sun.misc.Launcher$AppClassLoader class.

2. Class Loaders
   There are three primary types of class loaders:
   Bootstrap Class Loader: Loads core Java API classes from the JAVA_HOME/lib directory. It is implemented in native code and is not a Java object.
   Extension Class Loader: Loads classes from the JAVA_HOME/jre/lib/ext directory or any directory specified by the java.ext.dirs system property. It is implemented in Java.
   System/Application Class Loader: Loads classes from the application classpath, which is specified by the java.class.path environment variable. It is also implemented in Java.

3. JVM Memory Areas
  Method area: In the method area, all class level information like class name, immediate parent class name, methods and variables information etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource. 
  Heap area: Information of all objects is stored in the heap area. There is also one Heap Area per JVM. It is also a shared resource.
  Stack area: For every thread, JVM creates one run-time stack which is stored here. Every block of this stack is called activation record/stack frame which stores methods calls. All local variables of that method are stored in their corresponding frame. After a thread terminates, its run-time stack will be destroyed by JVM. It is not a shared resource.
  PC Registers: Store address of current execution instruction of a thread. Obviously, each thread has separate PC Registers.
  Native method stacks: For every thread, a separate native stack is created. It stores native method information. 

4. Execution Engine 
  Execution engine executes the “.class” (bytecode). It reads the byte-code line by line, uses data and information present in various memory area and executes instructions. It can be classified into three parts:
  Interpreter: It interprets the bytecode line by line and then executes. The disadvantage here is that when one method is called multiple times, every time interpretation is required.
  Just-In-Time Compiler(JIT): It is used to increase the efficiency of an interpreter. It compiles the entire bytecode and changes it to native code so whenever the interpreter sees repeated method calls, JIT provides direct native code for that part so re-interpretation is not required, thus efficiency is improved.
  Garbage Collector: It destroys un-referenced objects. For more on Garbage Collector, refer Garbage Collector.

5. Java Native Interface (JNI)
  It is an interface that interacts with the Native Method Libraries and provides the native libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.

6. Native Method Libraries
  These are collections of native libraries required for executing native methods. They include libraries written in languages like C and C++.

Q. What is JIT?
JIT stands for (Just-in-Time) compiler is a part of JRE(Java Runtime Environment), it is used for better performance of the Java applications during run-time. 
The use of JIT is mentioned in step by step process mentioned below:
1. Source code is compiled with javac to form bytecode
2. Bytecode is further passed on to JVM 
3. JIT is a part of JVM, JIT is responsible for compiling bytecode into native machine code at run time.
4. The JIT compiler is enabled throughout, while it gets activated when a method is invoked. For a compiled method, the JVM directly calls the compiled code, instead of interpreting it.
5. As JVM calls the compiled code that increases the performance and speed of the execution.

Q. What are Memory storages available with JVM?
JVM consists of a few memory storages as mentioned below:
1. Class(Method) Area: stores class-level data of every class such as the runtime constant pool, field, and method data, and the code for methods.
2. Heap: Objects are created or objects are stored. It is used to allocate memory to objects during run time.
3. Stack: stores data and partial results which will be needed while returning value for method and performing dynamic linking
4. Program Counter Register: stores the address of the Java virtual machine instruction currently being executed.
5. Native Method Stack: stores all the native methods used in the application.

Q. What is a classloader?
Classloader is the part of JRE(Java Runtime Environment), during the execution of the bytecode or created .class file classloader is responsible for dynamically loading the java classes and interfaces to JVM(Java Virtual Machine). 
Because of classloaders Java run time system does not need to know about files and file systems.

Q. What is Java String Pool?
A Java String Pool is a place in heap memory where all the strings defined in the program are stored. 
A separate place in a stack is there where the variable storing the string is stored. 
Whenever we create a new string object, JVM checks for the presence of the object in the String pool, If String is available in the pool, the same object reference is shared with the variable, else a new object is created.



Popular posts from this blog

IT Software Services

Agile Ceremonies

Learn Programming