Tuesday, 30 May 2017

What is Transient Keyword ?


Transient keyword is used along with instance variables to exclude them from serialization process.It provides you some control over serialization process and gives you flexibility to exclude some of object properties from serialization process.
  • While performing serialization if we don’t want to serialize the value of a particular variable then we should declare that variable with “transient” keyword.
  •  At the time of serialization JVM  ignores the original value of transient variable and save default value.
  • That is transient means “not to serialize”.
Important Points:
  • Transient keyword can not be used along with static keyword.Static variable is not part of object state hence they won’t participate in serialization because of this declaring a static variable as transient these is no use.
  • Final variables will be participated into serialization directly by their values. Hence declaring a final variable as transient there is no use.
  • Transient keyword can only be applied to fields or member variable. Applying it to method or local variable is compilation error.
  • Transient variable in java is not persisted or saved when an object gets serialized.

Sunday, 28 May 2017

Introduction To JVM

Introduction To JVM

What is JVM ?

The Java Virtual Machine (JVM) is a virtual machine that provides the runtime environment to execute Java bytecode.

The Since the JVM does not understand Java source code directly, *.java files are first compiled into .class files containing bytecode. This bytecode is platform-independent and can be executed by the JVM.

The JVM is responsible for controlling the execution of every Java program. It also enables important features such as:

  • Automatic exception handling
  • Garbage collection for memory management
  • Platform independence (via bytecode execution)

JVM Architecture : 

The JVM architecture consists of several key components that work together to execute Java programs:


1.Class LoaderThe Class Loader is responsible for loading classes into memory for execution.
There are three main types of class loaders:
  1. Boot Strap ClassLoader → Loads core Java classes from the rt.jar file (located in JRE/lib). It has the highest priority.
  2. Extension ClassLoader – → Loads classes from the ext directory (jre/lib/ext).
  3. Application (System) ClassLoader → Loads classes from the application’s classpath (environment variables, project paths, etc.).
2.Method area : All the class-level data will be stored here, such as class structures, metadata, method code, and static variables. There is only one Method Area per JVM instance.
3.Heap : 
All the Objects and their corresponding instance variables and arrays will be stored here. There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread safe.

4.Stack :
Each thread has its own JVM stack, created when the thread starts. It stores:
  • Local variables
  • Partial results
  • Method call frames (each frame holds data, local variables, and references to objects in the heap

5.Program Register : 
Each thread has its own PC register.It holds the address of the JVM instruction currently being executed.

6.Native method stack : 
Contains instructions for native methods (non-Java code, usually written in C/C++).Supports interaction between Java applications and native libraries.

7.Executive Engine :The Execution Engine is responsible for executing bytecode. It works in three parts:

  • Interpreter → Reads and executes bytecode line by line (slower execution).
  • JIT (Just-In-Time) Compiler → Improves performance by compiling frequently used bytecode into native machine code.
  • Garbage Collector (GC) → Automatically manages memory by removing unused objects from the heap.

8.Native Method Interface : Acts as a bridge between Java code and native applications/libraries (C, C++, etc.). Allows Java to call platform-specific native methods when needed.

9.Native Method Libraries
A collection of native libraries required by the Execution Engine. These libraries are loaded and linked using the Native Method Interface (JNI).

📝Key Features of JVM

  • Provides platform independence by executing bytecode on any system.
  • Ensures memory management with garbage collection.
  • Handles runtime exceptions automatically.
  • Supports integration with native libraries through JNI.

Java Development Kit (JDK) and Java Runtime Environment (JRE)

                  Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...