I have written following code to reverse the given String:
String str = "abcdef";
char[] strToChar = str.toCharArray();
char[] outString = new char[strToChar.length];
for(int j = 0; j < strToChar.length; ) {
int len = strToChar.length-j;
outString[j++] = strToChar[strToChar.length-j];
}
As per my understanding initialization happens from Right to Left. Hence, strToChar[strToChar.length-j]
here should throw ArrayIndexOutOfBoundException
.
But it runs fine. How is that happening? Shouldn't it evaluate to this?
outString[0] = strToChar[6]; //This should throw exception