Static methods and static variables storage

Question: Where do static methods and static variables get stored in memory?
Answer: Static variables and static methods are called class members. Static variables are common to all objects of that class. So instead of objects they are associated with the class and it is located in a fixed location memory. Hence any change made to the static variable is visible to all the objects. Both static variables and static methods are stored inside heap memory, but there is a special area of the heap called "Permanent Generation" (or PermGen).
Only the variables and their primitives or references are stored in Permanent Generation space. If a static variable is holding a reference to an object, then the reference would be stored in PermGen and the object would be stored in the normal heap space. The below example makes it more clear:
Since variable 'a' is a static int, its value 10 is stored in "Permanent Generation" space.
 static int a= 10;
 
Since variable 'obj' is a static object, it is stored in "Permanent Generation" space.
But, the object would be stored in heap memory.
 static MyObject obj = new MyObject();
 

User defined exception example

Exception Handling: user defined exception example
package com.ram;

import java.io.BufferedReader;
import java.io.InputStreamReader;

class MyException extends Exception{
 private static final long serialVersionUID = 1L;
 String except;

 public MyException(String str){
  except = str;
 }

 public String toString(){
  return "This is from MyException ..."+except;
 }
}

public class ExceptionHandlingExample3 {
 public static void main(String[] args){
  int a, b, c=0;

  try{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

   System.out.print("Enter value for a :: ");
   a = Integer.parseInt(br.readLine());

   System.out.print("Enter value for b :: ");
   b = Integer.parseInt(br.readLine());

   if(b==0){
    throw new MyException("Dividing by zero will result in exception ... enter a value other than 0 for 'b'");
   }else{
    c = a / b;
   }

   System.out.println("Value of c is :: "+c);
   
  }catch(MyException mye){
   System.out.println("Catching MyException ...");
   System.out.println(mye);
  }
  catch(Exception e){
   System.out.println("Other exceptions ... "+e);
  }
 }
}

 
 
Execute ExceptionHandlingExample3 class and user would be prompted to enter two values.
Enter value for a :: 10
Enter value for b :: 0
Catching MyException ...
This is from MyException ...Dividing by zero will result in exception ... enter a value other than 0 for 'b'

try-catch-finally example

Exception Handling: try-catch-finally example
 package com.ram;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExceptionHandlingExample2 {
 public static void main(String[] args) {
  String name = "";
  
  try{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   System.out.print("Entere a name:: ");
   name = br.readLine();
   char firstName[] = name.toCharArray();
   for(int i=0;i<=20;i++){
    System.out.print(firstName[i]);
   }
   
  }catch(ArrayIndexOutOfBoundsException aiobe){
   System.out.println(" An ArrayIndexOutOfBoundsException occured = "+aiobe);
  }
  catch(Exception e){
   System.out.println("Any other exception "+e);
  }finally{
   System.out.println("Name = "+name);
  }
 }
}

 
 
Execute ExceptionHandlingExample2 class and user would be prompted to enter a value.
Entere a name:: Sachin Tendulkar
Sachin Tendulkar An ArrayIndexOutOfBoundsException occured = java.lang.ArrayIndexOutOfBoundsException: 16
Name = Sachin Tendulkar

try-catch example

Exception Handling: try-catch example
 package com.ram;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExceptionHandlingExample1 {
 public static void main(String[] args){
  int a, b, c;

  try{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   System.out.print("Enter value for a :: ");
   a = Integer.parseInt(br.readLine());
   
   System.out.print("Enter value for b :: ");
   b = Integer.parseInt(br.readLine());
   
   c = a / b;
   System.out.println("C = "+c);
  }
  catch(ArithmeticException ae){
   System.out.println("An ArithmeticException occured = "+ae);
  }
  catch(Exception e){
   System.out.println("Any other exception "+e);
  }
 }
}

 
 
Execute ExceptionHandlingExample1 class and user would be prompted to enter two values.
To check for exception, enter "0" for variable "b"
Enter value for a :: 30
Enter value for b :: 0
An ArithmeticException occured = java.lang.ArithmeticException: / by zero

StringBuilder Example

StringBuffer example
 package com.ram;

public class StringBuilderExample {
 public static void main(String[] args) {
  //Creates an empty StringBuilder with a capacity of 16.
  StringBuilder sBuilder = new StringBuilder();
  
  sBuilder.append("There ");
  sBuilder.append("is ");
  sBuilder.append("no ");
  sBuilder.append("shortcut ");
  sBuilder.append("to success. ");
  sBuilder.append("Work ");
  sBuilder.append("Hard. ");
  sBuilder.append("- ");
  sBuilder.append("Sachin ");
  sBuilder.append("Tendulkar");
  
  //To display the StringBuilder
  System.out.println(sBuilder);
  
  //To get the length of the StringBuilder
  System.out.println(sBuilder.length());
  
  //To reverse StringBuilder
  System.out.println(sBuilder.reverse());
 }
}
 
 
Execute StringBuilderExample class and you get the below output:
There is no shortcut to success. Work Hard. - Sachin Tendulkar
62
rakludneT nihcaS - .draH kroW .sseccus ot tuctrohs on si erehT

StringBuffer example

StringBuffer example
 package com.ram;

public class StringBufferExample {
 public static void main(String[] args) {
  StringBuffer sBuffer = new StringBuffer();
  sBuffer.append("If");
  sBuffer.append(" you");
  sBuffer.append(" can");
  sBuffer.append(" dream");
  sBuffer.append(" it,");
  sBuffer.append(" you");
  sBuffer.append(" can");
  sBuffer.append(" do");
  sBuffer.append(" it");
  
  System.out.println("Complete sentence: \n"+sBuffer);
  
  //To know the length of StringBuffer
  System.out.println("Sentence length = "+sBuffer.length());
  
  //To get a character at a particular position
  System.out.println("Character at position 5 is :: "+sBuffer.charAt(5));
  
  //To get a part of string
  System.out.println(sBuffer.substring(21, 34));
  
  //To reverse StringBuffer
  System.out.println("Reverse  = "+sBuffer.reverse());
  
 }
}
 
 
Execute StringBufferExample class and you get the below output:
Complete sentence:
If you can dream it, you can do it
Sentence length = 34
Character at position 5 is :: u
you can do it
Reverse = ti od nac uoy ,ti maerd nac uoy fI

How ArrayList works internally

Question: How ArrayList works internally?
Answer:
ArrayList is like an array which can grow in memory dynamically. And we also know that ArrayList is not synchronized by default. In this post we will see how ArrayList works internally.
ArrayList<String> al = new ArrayList<String>();

This is how we declare an ArrayList. Here we did not specify any size for the list. Since we have not specified any value for the ArrayList, the default size is "10". Internally ArrayList uses Object[] array. The object array list looks like this:
 private transient java.lang.Object[] elementData;
 
When ArrayList is created the below piece of code gets executed.

this.elementData = new Object[initialCapacity];

We add items to an ArrayList using .add(); function. As we add items to ArrayList, the list checks the maximum size and if there is any space available then new element will be added. If there is no space left then then a new array would be created with 50% more space than the current ArrayList and all the data would be copied to the newly created array.