• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Don't understand how the index is becoming negative

 
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran the following code and....



got the error


i do not understand how the index becomes negative and how come it is going out of bounds at all.

Please help.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rrohit rakesh upadhyay wrote:
i do not understand how the index becomes negative and how come it is going out of bounds at all.



The error is occurring with this piece of code ...

When the i variable is something large (say one million), then the j variable will be larger (say one trillion, or one million times one million). Anyway, one trillion is much larger than can be held in an int variable. So, the variable will overflow -- and become negative.

Henry
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java ints are signed 32 bit numbers, stored 2's complement form. That means that an int can only store 2^32 (approx 4 billion) different values, and roughly half of those are negative. To view the largest possible int value print out Integer.MAX_VALUE.

Your code is calculating up to 1000000 squared, which is way bigger than Integer.MAX_VALUE. When you overflow the maximum possible int value you jump to the smallest possible int value (Integer.MIN_VALUE). So as soon as you get a value of i big enough such that i^2 is bigger than Integer.MAX_VALUE then your index becomes negative.

Edit: Beaten to it by Henry, I need to type faster.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, now i see it. Thank you very Henry and Mike for your explaination.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that √Integer.MAX_VALUE is approx 46340.95, so you don't have to go anywhere near the million before you get an overflow error.

This is a very common problem with all computerised integer arithmetic; have you not been taught about it?
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



This is not a Java specific standard. Java follows the Twos Complement standard with int (and related) types, and that standard has been around since (I would guess) the 1950s or 1960s.

Henry
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Byte is a 8 bits signed two's complement integer, which can hold values in range -128 and 127. So, byte type integer 127 is a 01111111 in two's complement notation (1st bit is a sign bit) .
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:
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Oracle Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:



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?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

No. You not simply invert the sign.

Lets take another example of 257. In binary it is 1 0000 0001 (9 bits). Since byte can hold only 8 bits, you take only 8 right most bits and you have left 0000 0001 in binary, which is 1 in two's complement notation. So assigning 257 to byte data type variable, after you print it out you'd get 1. Don't forget, in order to assign 257 you'd need to cast to byte "b = (byte)257", in this case you'd let Java know, that you know what you're doing and you can loose some precision.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rrohit rakesh upadhyay wrote: . . . if i assign a number that is greater than the upper bound,. . . .

You cannot have numbers greater than the largest bound. Try this and it won't compile:-
int i = 12345678987654321;
What happens is that the arithmetic produces something which won't fit into the 31 bits available; if you only go into the 32nd bit you change the sign of the number. If your arithmetic produces something which will only fit into 33+ bits, the bits over 32 disappear into cyber‑limbo never to be seen again and you end up with something which bears no apparent similarity to the expected result. Look at the following code which does repeated multiplications. Before you run it, tell me how many times it will print out:-You should be able to see where the overflow occurs and you cannot see how those results are obtained by multiplying the previous factorials, because 33rd bits and more have vanished.
 
reply
    Bookmark Topic Watch Topic
  • New Topic