ClassNotFoundException simply means that the class in not available in the CLASSPATH. You can see my previous post for tools to find the proper jar file containing the class. This exception is simpler to solve.
NoClassDefFoundError is little tricky. This means that the class is available in the CLASSPATH and still its definition could not be loaded. The most common reason for this issue is some problem with the static block of the class in question. If you have the source check the static block of the class or if you don't have the source find the jar file in local classpath using jarscan and decompile using JAD and review the static block.
Wednesday, April 30, 2008
ClassNotFoundException vs. NoClassDefFoundError
Friday, August 03, 2007
Array: new vs. java.lang.reflect.Array.newInstance
Since i was writing about array I thought this is also worth mentioning.
There are two ways of creating array
String str[] = new String[4];
String str[] = (String[]) java.lang.reflect.Array.newInstance(String, 4);
Both are same, you use second one where you can't use the first i.e. you don't know the type of array in advance.
Lets discuss Array Typecasting
You would think this code should work:
Object[] objs = new Object[2];
objs[0]="Hello";
objs[1]="World";
String[] strs = (Str[]) Objs;
// print the array..
Code compiles fine, but when you run it, you get java.lang.ClassCastException. And you start wondering why? String is subclass of Object so its not narrow conversion then why do i get ClassCastException. I tried to find answer on net but could not find, so below is just my own logic (disclaimer :-) )
String extends Object fine, but String[] doesn't extend Object[] both of them extend Object, in short they are not in hierarchy so you get ClassCastException. Lets look into more detail
Lets try System.out.println(strs.getClass()); which prints --> [Ljava.lang.String
and System.out.println(obj.getClass()); prints --> [Ljava.lang.Object
only common thing is [L, what is this?
Class Type that defines an Object consist of two things ClassType and ComponentType. For all array objects class name is same i.e. [L, but component types differs. Which implies Java Arrays are special type of classes whoes Class Type is represented by [L. If you had some way of setting the componentType, typecast would have worked. But it is pretty obvious that setComponentType can't be exposed.
Now finally I think we should discuss the solution to above problem. There are two solutions but none which could avoid an additional array creation.
Solution 1:
String[] strs = new String[objs.length];
System.arraycopy(objs,0,strs,0, objs.length);
Solution 2:
If you are using List you can use Object[] List.toArray(Object[])
//assuming you have done List list = Arrays.asList(objs); for our current example.
String[] strs = new String[2];
strs = (String[])list.toArray(strs);
Now if you look at solution 2 and wonder why typecasting worked here, it worked because ComponentType of the Array returned by toArray method has ComponentType same as the componentType of the Array passed to it. The argument is just used to get the comonentType
i.e. this would also work
strs = (String[])list.toArray(new String[0]);
Sunday, July 29, 2007
Reversing words in a String
This I think is by far most popular string Interview question, be it on Java/C/C++ or C#
Question : Write procedure to reverse the words in a String, assume words are separated by blank-spaces e.g. given a string "Writing and debugging software", the output of the procedure should be "software debugging and Writing"
Here is the code which i had written recently in Java, i think the code and algorithm is self-explanatory, but if you need help understanding the code please let me know.
If you have a big string and don't like the idea of O(n) space complexity and want to do it in O(1) space, than instead of putting words in word and to_ret arrays, you just store the array indexes and as soon as you find a blank space shift the original array up by the last word length and add the word to appropriate position in the string. This is just a hint i'll leave the implementation to you.. I can send you the code, if needed.
public class Reverse {
public static String reverse(String str) {
char[] chars = str.toCharArray();
char[] to_ret = new char[chars.length];
char[] word = new char[chars.length];
int l = 0;
int last = chars.length - 1;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
for (; l > 0; l--) {
to_ret[last] = word[l - 1];
last--;
}
to_ret[last] = ' ';
last--;
l = 0;
} else {
word[l] = chars[i];
l++;
}
}
//add the last word if any
if (l != 0) {
for (; l > 0; l--) {
to_ret[last] = word[l - 1];
last--;
}
}
return new String(to_ret);
}
public static void main(String[] args) {
String str = "This is a beautiful World";
System.out.println(str);
System.out.println(reverse(str));
}
}

