Tuesday, July 30, 2013

HashSet vs TreeSet

- Both Hashset and Treeset implement the java.util.set interface
- Since they implement the set interface, by contract they do not allow any duplicates
- Both the implementations are not synchronized and hence they are not thread-safe. However, they can be made thread-safe by invoking the Collections.synchronizedSet() method.
- The main difference between the two is ordering, i.e. TreeSet maintains an ordered collection, whereas a HashSet does not maintain order
- If your implementation does not require any order and needs a faster collection, then go for HashSet.
- One other main important difference is HashSet does allow null, whereas, TreeSet, does not allow any null objects.
- The reson for the above would be that HashSet use equals() method to compare two objects in a Set, whereas, TreeSet uses compareTo() method which compare keys and hence it throws a NullPointerException if any null object is inserted into a TreeSet. (needs to be confirmed)

Monday, July 29, 2013

Class Loaders

-Class loaders are java classes which is used to load class files in java from either a file system, network or any other source.
- There are three default class loaders used by java - bootstrap, extension and system class loaders of the Application Class loader.
- Every JVM has primordial or bootstrap class loaders which loads the java.* packages and rt.jar/i18n jar.
- There are non-primordial class loaders such as Extensions which loads the java extension packages.
- Then there is system class loaders, that loads all the class defined in the class path.
- Class loaders are hierarchical and follow a delegation model.
- The parent class has no visibility to child class, however, the child class has visibility to all the parent classes.
- Two sibling classes does not have visibility of each other, but they can see the parent class loaders.
- A class is never reloaded and during initialization the child request the parent to load the classes first and hence the uniqueness of class in maintained.

- class.getClassLoader.getParent() gets the parent class loader
- A class can be loaded by class.forName(class name) or class.forName(class name, initialized, class loader), based on which the classLoader calls the class.loadClass() method which in-turn calls the class.findClass() to locate the bytecodes of the actual class to load.

- It is recommended that any class which overrides Class Loader overrides this findClass instead of loadClass, since loadClass internally calls findClass for class loading.


Saturday, July 6, 2013

Difference between NoClassDefFoundError and ClassNotFoundException in Java

JVM throws NoClassDefFoundError when a static loading of class takes place and a class does not exist. Usually, this means that the class is present at the compile time and class loader isn't able to locate class at runtime. However, NoClassDefFoundError is mostly caused by dynamic loading of classes during runtime using reflection. Class.forName, ClassLoader.findSystemClass, ClassLoader.loadClass.

A detailed account of how to handle NoClassDefFoundError can be found here.. http://javarevisited.blogspot.com.au/2011/06/noclassdeffounderror-exception-in.html

Chief things to remember from the above blogpost -
a. Check for Class Path
b. Check for missing JAR
c. Check for JAR permissions
d. Since NoClassDefFoundError is sub-class of LinkageError, could be a linkage issue.
e. Check for Static load Initialization failure
f. Multiple Class loaders in J2EE environment
g. Check for typo in Bean initialization xml files

Java - Reverse a String

You can reverse a string using many methods in Java -
    String strToReverse = "Hello World";
a. Use StringBuffer reverse() method
    String reverse = new StringBuffer(strToReverse).reverse().toString();
b. Use StringBuilder reverse() method (However this is non-thread safe) method
    reverse = new StringBuilder(strToReverse).reverse().toString();
c. Use own logic to reverse string
    reverse = reverse(strToReverse);

    public static String reverse(String strToReverse) {
    String reverse = "";
     if(strToReverse == null || strToReverse.isEmpty()) {
             return strToReverse;
    }

    for(int i = strToReverse.length() - 1; i >=0 ; i--) {
        reverse = reverse + strToReverse.charAt(i);
    }
     return reverse;
  }

Monday, July 1, 2013

Java - Print Prime Number

Simple Program to Print prime numbers -

public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int number = 20;

for(int i=2 ; i<= number; i++) {
printPrime(i);
}
}

public static void printPrime(int number) {
boolean isNumberPrime = isNumberPrime(number);
if(isNumberPrime) {
System.out.println(number + " is Prime");
}
}

public static boolean isNumberPrime(int number) {
if(number == 1)
return true;

for(int i=2; i < number; i++) {
if(number%i == 0) {
return false;
}
}
return true;
}
}