I was testing how Java (SE7) would deal with the int that exceed its maximum value through the following code:
int index = 2147483647;//the maximum value of int
long size = 2147483648L; //More than the maximum value of int by 1
int safeCounter=0; //To prevent the infinite loop
while (index<size)
{
System.out.println("Index now is : "+index);//show the int value
index++; //increment the int value
safeCounter++; //increment the number of desired loops
if (safeCounter==3){
break;//to break the loop after 3 turns
}
}
and what I got is:
Index now is : 2147483647 Index now is : -2147483648 Index now is : -2147483647
So after being confused by this, (which if I don't use the safeCounter it would keep going forever between the maximum value and the minimum value of int -- and no exception is thrown) I was wondering how would an ArrayList handle a situation where the the number of elements exceed the maximum value of int (assuming that the heap space is not an issue)?
And if ArrayList can't handle this, Is there other data structure which can?
Can you also explain the behavior I got from the int variable?