Pages

Tuesday, 6 May 2014

Overflow And Underflow in Java

Overflow and underflow is a condition where you cross the limit of prescribed size for a data type. When overflow or underflow condition is reached, either the program will crash or the underlying implementation of the programming language will have its own way of handing things.

In Java the overflow and underflow are more serious because there is no warning or exception raised by the JVM when such a condition occurs.

Overflow in int

As int data type is 32 bit in Java, any value that surpasses 32 bits gets rolled over. It means that after incrementing 1 to 2147483647 (Integer.MAX_VALUE), the returned value will be -2147483648 (Integer.MIN_VALUE).

Underflow of int

Underflow is the opposite of overflow. While we reach the upper limit in case of overflow, we reach the lower limit in case of underflow. Thus after decrementing 1 from -2147483648 (Integer.MIN_VALUE), we reach 2147483647 (Integer.MAX_VALUE). Here we have rolled over from the lowest value of int to the maximum value.

Overflow and Underflow in Java floating point operators

While using Java floating point operators, overflow will result in Infinity and underflow will result 0.0.

No comments:

Post a Comment