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.

Sunday, 8 January 2017

Abstarction :

Abstraction is process of hiding the implementation details and showing only the functionality.
·Abstraction in java is achieved by using interface and abstract class. Interface give 100% abstraction and abstract class give 0-100% abstraction.


Real Time Example :

  • Take the same example of news channel. The article they write on newspaper is abstracted as the heading. Hence the simple heading of the whole article is abstracted.
  • All are performing operations on the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't know internal details about ATM.

Friday, 11 November 2016

Association ,Composition and Aggregation

Association, Composition and Aggregation

1. Association :

    • Association is a relationship between two or more classes (objects) that shows how they are connected.
    • Unlike composition or aggregation, association does not imply ownership or dependency — it just represents a link.
    • Associations can be:
      • One-to-One
      • One-to-Many
      • Many-to-One
      • Many-to-Many

    ๐Ÿ“˜ Real-life Example :

    • Student is associated with a Faculty.
    • The Faculty teaches Students, and Students learn from the Faculty.
    • Both can exist independently, but they share a relationship.
    UML Diagram :

    Student  ↔  Faculty
    (Simple line = Association)
    Example :

    class Student {
        private String name;
    
        Student(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    
    class Faculty {
        private String name;
    
        Faculty(String name) {
            this.name = name;
        }
    
        public void teach(Student student) {
            System.out.println(name + " teaches " + student.getName());
        }
    }
    
    public class AssociationExample {
        public static void main(String[] args) {
            Student s1 = new Student("Ravi");
            Faculty f1 = new Faculty("Dr. Sharma");
    
            // Association: Faculty teaches Student
            f1.teach(s1);
        }
    }
    Output :

    Dr. Sharma teaches Ravi
    ๐Ÿ”‘ Key Point
    • Association just connects objects — neither object’s lifecycle depends on the other.
    • If one is destroyed, the other can still exist.

    Composition vs Aggregation

    When designing classes in Object-Oriented Programming (OOP), we often deal with “has-a” relationships. These can be either Composition or Aggregation. Although both represent associations, the key difference lies in the dependency of objects.

    ✅ 2. Composition

    • Definition: A strong “has-a” relationship.
    • The contained object cannot exist without the container.
    • If the container is destroyed, the contained objects are also destroyed.

    ๐Ÿ“˜Real-life Example:

    • University consists of several Departments.
    • If the University object is destroyed, all its Department objects will also be destroyed.
    • Since a Department cannot exist without its University, this is a strong association, i.e., composition.
    UML Diagram :

    University ◆── Department
    (black diamond = Composition)
    Example :

    class Department {
        private String name;
    
        Department(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    
    class University {
        private List<Department> departments;
    
        University(List<Department> departments) {
            this.departments = departments;
        }
    
        public void showDepartments() {
            for (Department d : departments) {
                System.out.println(d.getName());
            }
        }
    }
    
    public class CompositionExample {
        public static void main(String[] args) {
            Department d1 = new Department("Computer Science");
            Department d2 = new Department("Mechanical");
            List<Department> deptList = List.of(d1, d2);
    
            University university = new University(deptList);
            university.showDepartments();
            // When university is destroyed, departments go away too.
        }
    }

     3. Aggregation :

    • Definition: A weak “has-a” relationship.
    • The contained object can exist independently of the container.
    • Destroying the container does not destroy the contained objects.

      ๐Ÿ“˜ Real-life Example

      • Department may have several Professors.
      • If the Department is closed, the Professor objects can still exist independently (they may move to another department).
      • Since a Professor can exist without a specific Department, this is a weak association, i.e., aggregation.

      UML Diagram :

      Department ◇── Professor
      (white diamond = Aggregation)
      Example :

      class Professor {
          private String name;
      
          Professor(String name) {
              this.name = name;
          }
      
          public String getName() {
              return name;
          }
      }
      
      class DepartmentWithProfessor {
          private List<Professor> professors;//Aggregation
      
          DepartmentWithProfessors(String deptName, List<Professor> professors) {
              this.deptName = deptName;
              this.professors = professors;
          }
      
          public void showProfessors() {
              System.out.println("Department: " + deptName);
              for (Professor p : professors) {
                  System.out.println("Professor: " + p.getName());
              }
          }
      }
      
      public class AggregationExample {
          public static void main(String[] args) {
              Professor p1 = new Professor("Dr. Sharma");
              Professor p2 = new Professor("Dr. Singh");
      
              List<Professor> profList = List.of(p1, p2);
      
              DepartmentWithProfessors dept = new DepartmentWithProfessors("Computer Science", profList);
              dept.showProfessors();
              // Even if Department is removed, Professors still exist.
          }
      }


      ๐Ÿ”‘ Key Differences Between Composition and Aggregation:

      Feature

      Composition (Strong)

      Aggregation (Weak)

      Dependency

      Contained objects depend on container

      Contained objects independent

      Lifetime

      Destroyed when container is destroyed

      Can exist even if container is destroyed

      UML Notation

      Black Diamond ()

      White Diamond ()

      Example

      University – Departments

      Department – Professors


      ๐Ÿ“ Final Thoughts

      • Association is a general relationship between two objects. Both can exist independently, but they are linked (e.g., StudentFaculty).
      • Aggregation is a special type of association where objects have a weak dependency (e.g., DepartmentProfessor). The contained object can still exist independently.
      • Composition is a strong form of association where objects have a strong dependency (e.g., UniversityDepartment). If the container is destroyed, the contained objects are destroyed too.

      Monday, 31 October 2016

      Polymorphism

      Polymorphism

      Polymorphism means “many forms” (poly = many, morph = forms).
      It is the ability of an object or method to take different forms and perform a single action in multiple ways.

      In Java, polymorphism is categorized into two types:

      1. Compile-time Polymorphism (also called Static Polymorphism)

      2. Runtime Polymorphism (also called Dynamic Polymorphism)

      Polymorphism in Java can be achieved through:



      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 ) ...