Monday, May 23, 2016


welcome to jsf hello world in tomcat with eclipse using mojarra FCS jsf 2.0 file....... New......dynamic web project.... project name===== hellojsf Refferences Go to tutorial

Ways to create object in java


there are six different ways to create object in java
  1. new keyword
  2. class.forName()=====CrunchifyObj object2 = (CrunchifyObj) Class.forName("crunchify.com.tutorial.CrunchifyObj").newInstance();
  3. clone()
  4. newInstance() ====CrunchifyObj.class.getClassLoader().loadClass("crunchify.com.tutorial.CrunchifyObj").newInstance();
  5. Deserialization
  6. use the Constructor class from the java.lang.reflect

for more details


ClassNotFoundException vs NoClassDefFoundError


Though both of these errors are related to missing classes in the classpath, the main difference between them is their root cause.

ClassNotFoundExcpetion comes when you try to load a class at runtime by using Class.forName() or loadClass() and requested class is not present

in classpath for example when you try to load MySQL or Oracle driver class and their JAR is not available, while in case of

NoClassDefFoundError requested class was present at compile time but not available at runtime. Sometimes due to an exception during class

initialization e.g. exception from static block causes NoClassDefFoundError when a failed-to-load class was later referenced by the runtime

for more details

Sunday, May 22, 2016


Difference between ArrayList and LinkedList in Java


for more details 1. Implementation : ArrayList is the resizable array implementation of list interface , while LinkedList is the Doubly-linked list implementation of the list interface. 2. Performance : Performance of ArrayList and LinkedList depends on the type of operation a. get(int index) or search operation : ArrayList get(int index) operation runs in constant time i.e O(1) while LinkedList get(int index) operation run time is O(n) . The reason behind ArrayList being faster than LinkedList is that ArrayList uses index based system for its elements as it internally uses array data structure , on the other hand , LinkedList does not provide index based access for its elements as it iterates either from the beginning or end (whichever is closer) to retrieve the node at the specified element index. b. insert() or add(Object) operation : Insertions in LinkedList are generally fast as compare to ArrayList. In LinkedList adding or insertion is O(1) operation . While in ArrayList, if array is full i.e worst case, there is extra cost of resizing array and copying elements to the new array , which makes runtime of add operation in ArrayList O(n) , otherwise it is O(1) . c. remove(int) operation : Remove operation in LinkedList is generally same as ArrayList i.e. O(n). In LinkedList , there are two overloaded remove methods. one is remove() without any parameter which removes the head of the list and runs in constant time O(1) . The other overloaded remove method in LinkedList is remove(int) or remove(Object) which removes the Object or int passed as parameter . This method traverses the LinkedList until it found the Object and unlink it from the original list . Hence this method run time is O(n). While in ArrayList remove(int) method involves copying elements from old array to new updated array , hence its run time is O(n). 3. Reverse Iterator : LinkedList can be iterated in reverse direction using descendingIterator() while there is no descendingIterator() in ArrayList , so we need to write our own code to iterate over the ArrayList in reverse direction. 4. Initial Capacity : If the constructor is not overloaded , then ArrayList creates an empty list of initial capacity 10 , while LinkedList only constructs the empty list without any initial capacity. 5. Memory Overhead : Memory overhead in LinkedList is more as compared to ArrayList as node in LinkedList needs to maintain the addresses of next and previous node. While in ArrayList each index only holds the actual object(data).

Friday, May 20, 2016

Hashmap bucket concept with Hashcode and equals method implementation

Bucket concept of HashMap
please look how the bucket id is calculated for a particular key ------------
Hashtable: obj.hashCode() % Hashtable.length
where
legth==capacity for the hashtable or Hashmap or no of bucket
Default no of buckets or capacity for Hashmap is == 16 with 0.75 load factor
load factor 0.75 means on 13th insert again it will grows the buckets with specified capacity


Hashcode and equals Contract example with two different map object having same hashcode for same keys and same values for both the Maps 

public class Test {
    public static void main(String[] args) {

        Map map = new HashMap();
        map.put(1, 11);
        map.put(4, 11);
        System.out.println(map.hashCode());
        Map map1 = new HashMap();
        map1.put(1, 11);
        map1.put(4, 11);
        System.out.println(map1.hashCode());
        if (map.equals(map1)) {
            System.out.println("equal ");
        }
    }
}


click for more details for bucket ID

Thursday, May 5, 2016

What is JAVA ? Java is pure object oriented,platform independent ,distributed,multi threaded,web based ,secure and robust programming language used world wide.