Object class

Object class is present in the java.lang package. It is the super class for all the classes directly or indirectly. If a class is not extended any class then for the class Object class is super class. If a class extends an other class then for this class Object class is indirect super class.

Object class has 12 methods which given below.

  1. private static native void registerNatives(): It is a private method so we no need to consider it. It is implemented in the other language(C/C++).
  2. public final native Class<?> getClass(): It is a native method and implementation in other language(C/C++). Which is used for the getting the class level information. It also provides the meta data of a class. It is a final class and it can’t be overridden.Example:
  3. public native int hashCode(): It is a native method. For every object JVM generates a unique number which is different for different objects. This distinct number is generated by the hashcode. hashCode method returns the unsigned hex decimal integer. This number is formed by hashing the internal address of the object by using an algorithm. The main advantage of hashing is searching becomes easy. This method can be overridden, when it overridden we have to make sure that each object will have different hashcode.
  4. public boolean equals(Object obj): It is used for compare a object with ‘this’ object. By default implementation in Object class of this method is compare the given object reference with ‘this’ object reference (Not contents of the object).  It is recommended that whenever this method is overridden then override hashcode method for make sure same content objects has same hash code.
  5. protected native Object clone(): It is used for the make a copy of the object. If we want to make a copy of an object with using this method then class must implemented with Cloneable interface(Marker Interface) otherwise will get a CloneNotSupportedException.  By using clone method we can do 2 types of object copies. 1. Shallow cloning ( When we use it, it can copy only instance variables but not reference variables. i.e if we try to modify using this reference variables from copied object then actual object of that reference will be modified). 2. Deep cloning (When we use it, it can copy instance variables and also reference variable. i.e if we modify the data with reference variable from the copied object then it won’t modify the actual object.)
  6. public String toString(): It is used for converting the object to String. By default implementation of this method in Object class is to convert the object reference to String. i.e class name + @+ hex integer. Not actual value of object. It is recommended to override to get the content of object to convert String.
  7. public final void wait(): It is used in the multi-threaded programming. It keeps the current thread in sleep and release the lock until another thread enters the same monitor and calls notify.
  8. public final void wait(long timeout): It keeps the current thread in sleep for specified time and release the lock on it. Here time is milliseconds.
  9. public final void wait(long timout, int nanos): It keeps the current thread in sleep for specified milliseconds and nanoseconds and releases the lock on it.
  10. public final native void notify(): It wakes up(notify) one thread which is called wait on same object. notify method just wakes up the thread but not release the lock until its execution finished.
  11. public final native void notifyAll():It wakes up all threads which are called wait on same object. It also doesn’t release the lock until execution is finished.

Note: Above 5 methods (from 7 to 11) used in synchronized block only.

12.protected void finalize(): This method is called just before an object is garbage collected. It is called by the Garbage Collector on an object when garbage collector determines that there are no more references to the object. This method we need to override whenever we need to clean up the resources used in the object and also to avoid the memory leaks.

Advertisements

JVM

JVM(Java Virtual Machine): JVM is a part of JRE(Java Runtime Enviroment). Jvm is responsible for call the main method present in java code.

Image Source: Wikipedia.

When we compile .java file it will generate .class(Byte code) with same name. When we run it it will go to several steps, these steps together describes JVM.

Class Loader: Class loader loads the class information in the method area. Class loader responsibilities are below.

  1. Loading
  2. Linking
  3. Initialization

Loading: Class loader reads the .class file and generates the binary data then save it in the method area. It loads the following information in the method area for every .class file.

-> Fully qualified class name and its immediate super class name.

-> Whether .class file related to Class or Interface or Enum.

-> Modifiers, variables and method information etc.

After loading .class file JVM creates the object for the Class which is from java.lang package. This Class object represents the class loaded .class file. Using this object we can get the class level information of the loaded class. We can get Class object from the getClass method of Object class.

Linking: It performs verification, preparation and resolution.

-> It verifies the correctness of the .class file. i.e it checks the file format is proper or not and also is it compiled with proper compiler or not. If verification fails then we will get java.lang.VerifyError runtime exception.

-> It allocates the memory for the instance variables and assign them with their default values.

Initialization: In this phase all the static variables assigned with its values and static blocks if any from the top to button and from the parent class to child class.

Basic class loader are below.

  1. Bootstrap class loader: It is capable of loading trusted classes. It loads core java api classes which are present in the jre/lib directory. This directory popularly know as bootstrap path. Every JVM must implement bootstrap class loader. It is implemented in native languages like C/C++.
  2. Extension class loader: It is a child class loader of bootstrap class. It is responsible for loading the classes which is present in the jre/lib/ext directory or java.ext.dirs property set path. It is implemented by the sun.misc.Launcher$ExtClassLoader class.
  3. Application/System class loader: It is a child class loader of extension class loader which is responsible for load the class present in the application path. It is internally uses the class path(java.class.path). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

JVM follow Delegation-Hierarchy principle to load classes.

https://i0.wp.com/d1hyf4ir1gqw6c.cloudfront.net//wp-content/uploads/JVM1.png

Image Source: http://javarevisited.blogspot.in/2012/12/how-classloader-works-in-java.html

JVM Memory:

Method Area: In method area classes information will be stored like class name, immediate super class name, variables(including static variables) and method informations. Only one method area per JVM and it is shared memory.

Heap Memory: Information of all the objects will be present here. Only one heap area per JVM and it is shared memory.

Stack Memory: For every thread JVM creates a stack memory, here local variables of the method will be stored and when thread is terminated the corresponding stack memory will be cleared by the JVM. It is not a shared memory.

PC Registers: Each thread as its own pc registers and it holds the address of current execution of a thread.

Native Method Stack: It stores the native methods information. Every thread has its own native method stack.

Execution Engine: It reads the .class file(byte code) line by line and use the data or information present in different memory areas then executes the instructions.

Execution engine consists of 3 parts.

  1. Interpreter: It interprets the byte code line by line then executes. It needs more time when we call method multiple time for interpretation.
  2. Just In Time(JIT) compiler: It compiles the byte code and converts it into native code, so whenever interpreter requires multiple class then JIT provides native code to the interpreter so re-interpretation is not required. Because of it efficiency of interpreter will be improved.
  3. Garbage Collector: It is used for the clear the memory of object which are no longer used and their reference is referred to null.

Java Native Interface(JNI): It is a interface which interacts with the Native method libraries and it provides this libraries for execution.

Native Method Libraries: These are native libraries developed in C/C++ which are required for the execution engine.

String reverse with recursion

Steps:

  1. Create a method which receives String as argument and return String.
  2. Check is string is null or string length is less or equal to 1 then return the current string only.
  3. If string length is more then 1 then again call string reverse method with substring(1) and append it with charAt(0). So first character of each call will be appended from right to left.

Example: “abc” is string then

substring of 1 is bc + ‘a’.

substring of 1 is c+’b’+’a’

Example:

package com.gudla.randomprograms;

/**
 * Created by santhosh on 28/3/17.
 */
public class StringReverse {

    public static void main(String[] args) {
        System.out.println(reverse("android"));

    }

    private static String reverse(String s){
        if(s == null || s.length() <= 1 ) {
            return s;
        } else {
            return reverse(s.substring(1))+s.charAt(0);
        }
    }
}

Output:

diordna

Swap 2 Numbers

We can swap 2 numbers in different ways.

  1. Using a temp variable.
  2. Without using temp variable
  3. Using Logical operators
  4. In Single line

Example:

package com.gudla.swapnumbers;

/**
 * Created by santhosh on 26/3/17.
 */
public class SwapNumbers {

    public static void main(String[] args) {
        withTemp(10, 20);
        System.out.println("\n"+"\n");
        withoutTempSimple(10, 20);
        System.out.println("\n"+"\n");
        withLogical(10, 20);
        System.out.println("\n"+"\n");
        inSingle(10, 20);
    }

    private static void inSingle(int a, int b) {
        System.out.println("Before a value is : "+a);
        System.out.println("Before b value is : "+b);
        System.out.println("\n");
        b=(a+b)-(a=b);

        System.out.println("After a value is : "+a);
        System.out.println("After b value is : "+b);

    }

    private static void withLogical(int a, int b) {
        System.out.println("Before a value is : "+a);
        System.out.println("Before b value is : "+b);
        System.out.println("\n");
        a = a^b;
        b = a^b;
        a = a^b;
        System.out.println("After a value is : "+a);
        System.out.println("After b value is : "+b);
    }

    private static void withoutTempSimple(int a, int b) {
        System.out.println("Before a value is : "+a);
        System.out.println("Before b value is : "+b);
        System.out.println("\n");
        a = a+b;
        b = a-b;
        a = a-b;
        System.out.println("After a value is : "+a);
        System.out.println("After b value is : "+b);
    }

    private static void withTemp(int a, int b) {
        System.out.println("Before a value is : "+a);
        System.out.println("Before b value is : "+b);
        System.out.println("\n");
        int c = a;
        a = b;
        b = c;

        System.out.println("After a value is : "+a);
        System.out.println("After b value is : "+b);
    }
}

Output:

Before a value is : 10
Before b value is : 20

After a value is : 20
After b value is : 10

Before a value is : 10
Before b value is : 20

After a value is : 20
After b value is : 10

Before a value is : 10
Before b value is : 20

After a value is : 20
After b value is : 10

Before a value is : 10
Before b value is : 20

After a value is : 20
After b value is : 10

Vector

Vector is a class which extends AbstractList and implements List interface.

  1. Vector is a Legacy collections class. Which is introduced in Java 1.0.
  2. Vector is synchronized, so it can be used in multi threading.
  3. Vector implements Random access interface so we can access the elements from the vector randomly.
  4. Initial capacity of vector is “10” . When it reaches to max then capacity will be increased to double.
  5. All vector specific methods are synchronized.

Constructors:

  1. Vector(): Creates empty Vector with initial capacity “10”.
  2. Vector(int capacity): Creates empty vector with specified capacity.
  3. Vector(int capacity, int incrementalCapacity): Creates empty vector with specified capacity and when it reaches to max capacity then add the incrementalCapacity.newCapacity = oldCapacity + incrementalCapacity ;
  4. Vector(Collection c): It creates the Vector which will contain all the elements of the specified Collection.