Thursday, April 26, 2007

is all string manipulations requires synchronization?

The Java language specification assures us that strings are immutable, so most operations on a string destroy the original object and create a new one. But in reality the compiler can optimize the operation and reuse the same memory as the original string. For instance we have a string ‘String stringSample = "Saravanan";’ stored in memory at memory location m1. The compiler will store this String in memory as origin location = m1 and offset = 12.

Now when an operation ‘stringSample = stringSample.substring(4);’ is done, stringSample will contain "vanan".

As per the Java specification we would assume that stringSample will be moved to a new location m2 and the old memory location is left for the garbage collector, but compilers can reuse the same memory location and update the offsets. So the new string after the sub string operation would read as origin location = m1+4 and offset = 8. This does not break the Java specification, as the old reference is lost and a new reference is created with a new origin location and a new offset. The problem is when two threads try to access this string at the same time; the second thread might get the wrong starting location.

Tuesday, April 10, 2007

is this a java bug?

class ArrayTest{
public static void main(String arg[]) {
int a[]=new int[0];
a[0]=5;
}
}
It will compile fine,when I run this code,its saying ArrayIndexOutOfBoundException....
As we know array declaration should have one integer value,even if I give -1 also it will compile. But in runtime it is saying NegativeIndex or ArrayIndexOutOfBound for -1 or 0 respectivley.

Why it is doing like this? While doing compiling itself why don't the compiler has do that? I accept for the variable initialization since it has clue for the variable in the compilation time,but for straight forward initialization like above should caught by the compiler... is my understanding is wrong?