Class Loader Sub System

Class loader sub system
First of all, it loads the .class file.
Then it verifies whether all byte code instructions are proper or not. If it finds any instruction suspicion, the execution is rejected immediately.
If the byte instructions are proper, then it allocates necessary memory to execute the program. This memory is divided into 5 parts, called run time data access, which contains the data and results while running the program.
Method area: Stores the class code, code of the variables and code of the methods in the program.
Heap: Area where objects are created. Whenever jvm loads a class, a method and a heap area are immediately created in it.
Java stacks: Method code is stored on method area. But while running a method, it needs some more memory to store the data and results. This memory is allocated on java stacks. So these are the memory areas where java methods are executed. While executing methods, a separate frame will be created in the java stack, where the method is executed. Jvm uses a separate thread or process to execute each method.
PC (Program counter) registers:
These are the registers (memory areas), which contain memory address of the instructions of the methods. If there are 3 methods, 3 pc registers will be used to track the instructions of the methods.
Native method stacks: Java methods are executed on java stacks. Similarly native methods (C/C++) are executed here.

Execution engine contains interpreter and JIT compiler which are responsible for converting the byte code instructions into a machine code.

Enum example

Enum example
 package com.ram;

enum Continents{
 AUSTRALIA, ASIA, EUROPE, AFRICA, NORTH_AMERICA, SOUTH_AMERICA, ANTARCTICA 
}

public class EnumExample {
 
 Continents continents;
 
 public EnumExample(Continents continents){
  this.continents = continents;
 }
 
 public void tellAboutContinents(){
  switch(continents){
   case AUSTRALIA:
    System.out.println("Australia is the largest island. Australia's Great Barrier Reef is the world's largest coral reef.");
    break;
    
   case ASIA:
    System.out.println("It is the largest continent. It is the home of the 10 highest mountain peaks in the world.");
    break;
   
   case EUROPE:
    System.out.println("In Europe, there are no deserts.It is the only continent without any deserts.");
    break;
   
   case AFRICA:
    System.out.println("Africa is very rich in minerals.Ninety five percent of the worlds’s diamonds and more than 50% of the world’s gold comes from Africa.");
    break;
    
   case NORTH_AMERICA:
    System.out.println("North America was named after the explorer Americo Vespucci. North America is the only continent that has every kind of climate.");
    break;
    
   case SOUTH_AMERICA:
    System.out.println("The Angel Falls of South America ,is the Highest Waterfall in the World.");
    break;
    
   case ANTARCTICA:
    System.out.println("Antarctica is a frozen land area around the South Pole.It is also called Frozen Continent.");
    break;
   
   default:
    System.out.println("Rest is ocean ...");
  }
 }
 
 public static void main(String[] args) {
  EnumExample australia = new EnumExample(Continents.AUSTRALIA);
  australia.tellAboutContinents();
  EnumExample asia = new EnumExample(Continents.ASIA);
  asia.tellAboutContinents();
  EnumExample europe = new EnumExample(Continents.EUROPE);
  europe.tellAboutContinents();
  EnumExample africa = new EnumExample(Continents.AFRICA);
  africa.tellAboutContinents();
  EnumExample northAmerica = new EnumExample(Continents.NORTH_AMERICA);
  northAmerica.tellAboutContinents();
  EnumExample southAmerica = new EnumExample(Continents.SOUTH_AMERICA);
  southAmerica.tellAboutContinents();
  EnumExample antarctica = new EnumExample(Continents.ANTARCTICA);
  antarctica.tellAboutContinents();
  
 }
}
 
 
 
Execute EnumExample class and you get the below output:

Australia is the largest island. Australia's Great Barrier Reef is the world's largest coral reef.
It is the largest continent. It is the home of the 10 highest mountain peaks in the world.
In Europe, there are no deserts.It is the only continent without any deserts.
Africa is very rich in minerals.Ninety five percent of the worlds’s diamonds and more than 50% of the world’s gold comes from Africa.
North America was named after the explorer Americo Vespucci. North America is the only continent that has every kind of climate.
The Angel Falls of South America ,is the Highest Waterfall in the World.
Antarctica is a frozen land area around the South Pole.It is also called Frozen Continent.

Print "*" in a pyramid format

Pyramid example
 package com.ram;

public class Pyramid {
 public static void main(String[] args) {
  System.out.println("Program to print a character '*' in the form of a pyramid");
  System.out.println("Assuming we need a pyramid of size 10");
  //In order to print the triangle, we have to split the triangle into two parts.
  //The first will have a for loop which will print spaces (" ")
  //The second will have another for loop which will print "* "
  for(int i=0; i<10; i++){
   for(int j=i; j<10; j++){
    System.out.print(" ");
   }
   
   for(int k=0; k<=i; k++){
    System.out.print("* ");
   }
   
   System.out.println();
  }
 }
}
 
 
Execute Pyramid class and you get the below output:

Login page using Spring & Hibernate

HashMap example

HashMap example
 package com.ram;

import java.util.HashMap;
import java.util.Iterator;

public class HashMapExample {
 public static void main(String[] args) {
  HashMap map = new HashMap();
  //Men's ATP rankings as of 2014
  map.put(1, "Nadal");
  map.put(2, "Djokovic");
  map.put(3, "Ferrer");
  map.put(4, "Murray");
  map.put(5, "Del Porto");
  map.put(6, "Federer");
  
  //Get the size of HashMap
  System.out.println("Size of of HashMap: "+map.size());
  
  //Returns value if key is present
  System.out.println(map.get(2));
  
  //Retrieve values from HashMap
  Iterator it = map.keySet().iterator();
  while(it.hasNext()){
   Integer key = it.next();
   System.out.println(map.get(key)+" rank is : "+key);
  }
  
  //Returns true if the value is present
  System.out.println(map.containsValue("Murray"));
  
  //Returns true if key is present
  System.out.println(map.containsKey(6));
  
  //Removes key and its corresponding value if the key is present
  System.out.println("Removed "+map.remove(3));
 
  //Removes all mappings from map
  map.clear();
 }
}

 
Execute HashMapExample class and you get the below output:
Size of of HashMap: 6
Djokovic
Nadal rank is : 1
Djokovic rank is : 2
Ferrer rank is : 3
Murray rank is : 4
Del Porto rank is : 5
Federer rank is : 6
true
true
Removed Ferrer

ArrayList example

ArrayList example
 package com.ram;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExample {
 public static void main(String[] args) {
  //Create an ArrayList
  ArrayList<String> al = new ArrayList<String>();
  
  al.add("Coke");
  al.add("Pepsi");
  al.add("Sprite");
  al.add("Mountain Dew");
  al.add("Fanta");
  al.add("Red Bull");
  
  //Get the size of ArrayList
  System.out.println("Size of ArrayList: "+al.size());
  
  //Display elements of ArrayList
  System.out.println("ArrayList elements: "+al);
  
  //Retrieve an element using its index
  System.out.println(al.get(5));
  
  //Replace an element at the specified position in the list with the specified element
  al.set(0, "Coca-cola coke");
  System.out.println("ArrayList elements: "+al);
  
  //Inserts an element at the specified location.
  al.add(2, "ThumsUp");
  System.out.println("ArrayList elements: "+al);
  
  //Checks if the element "Fanta" is in the list. If present will return true else false.
  System.out.println(al.contains("Fanta"));
  
  System.out.println(al.remove("Fanta"));
  
  //Display ArrayList elements using iterator
  Iterator<String> it = al.iterator();
  while(it.hasNext()){
   System.out.println(it.next());
  }
  
  //Removes all elements from the list
  al.clear();
  System.out.println("Size of ArrayList: "+al.size());
 }
}

 
Execute ArrayListExample class and you get the below output:
Size of ArrayList: 6
ArrayList elements: [Coke, Pepsi, Sprite, Mountain Dew, Fanta, Red Bull]
Red Bull
ArrayList elements: [Coca-cola coke, Pepsi, Sprite, Mountain Dew, Fanta, Red Bull]
ArrayList elements: [Coca-cola coke, Pepsi, ThumsUp, Sprite, Mountain Dew, Fanta, Red Bull]
true
true
Coca-cola coke
Pepsi
ThumsUp
Sprite
Mountain Dew
Red Bull
Size of ArrayList: 0

Dynamic binding example

Dynamic binding example
Dynamic binding: It is a case in java where compiler is unable to decide which method to call at compilation time. Only JVM decides which method is called at runtime. Below program is an example of "dynamic binding" or "late binding".
 package com.ram;

interface Area{
 public int area(int length);
}

class Square implements Area{

 @Override
 public int area(int length) {
  int squareArea = length * length;
  return squareArea;
 }
}

class Circle implements Area{

 @Override
 public int area(int length) {
  double circleArea = (22.0/7.0) * length * length;
  return (int)circleArea;
 }
}

public class CalculateArea {
 public static void main(String[] args){
  Area areaObj1 = new Square();
  Area areaObj2 = new Circle();
  
  System.out.println("Square area = "+areaObj1.area(10));
  System.out.println("Circle area = "+areaObj2.area(10));
 }
}
 
 
Execute CalculateArea class and you get the below output:
Square area = 100
Circle area = 314