Rrohit rakesh upadhyay wrote:
i do not understand how the index becomes negative and how come it is going out of bounds at all.
Rrohit rakesh upadhyay wrote:I knew about the limits of the int data type in java but i did not know that it is assigned negative value if overflow happens.
And what is interesting, that these negative values are not random numbers. Lets take simple example to understand what is happening.Rrohit rakesh upadhyay wrote:I knew about the limits of the int data type in java but i did not know that it is assigned negative value if overflow happens.
What happens if you try to store for instance integer 128 (remember, max value can store is 127) in byte data type. So you get 10000000 in binary. As byte is only 8 bits long, 1st bit is a sign bit (1 negative, 0 positive), so in this case you get -128 in two's complement notation.
And you could try this code to see it yourself:
No. You not simply invert the sign.Rrohit rakesh upadhyay wrote:Does this mean if i assign a number that is greater than the upper bound, the two's compliment of that number is calculated and the binary number that we get after two's compliment (10000000 in case of 128) is just represented as an integer with a negative sign?
You cannot have numbers greater than the largest bound. Try this and it won't compile:-Rrohit rakesh upadhyay wrote: . . . if i assign a number that is greater than the upper bound,. . . .