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 arithmetic operators don’t report overflow and underflow conditions. They simply swallow it! It is a very dangerous thing to do. If one doesn’t know how Java handles overflow and underflow then he will not be aware of things happening behind while doing arithmetic operations.
Arithmetic integer operations are performed in 32-bit precision. When the resultant value of an operation is larger than 32 bits (the maximum size an int variable can hold) then the low 32 bits only taken into consideration and the high order bits are discarded. When the MSB (most significant bit) is 1 then the value is treated as negative.
While using java floating point operators, overflow will result in Infinity and underflow will result 0.0 As a general rule here also Java doesn’t throw an error or exception for overflow and underflow.
public class OverflowUnderflow { public static void main(String args[]){ //roll over effect to lower limit in overflow int overflowExample = 2147483647; System.out.println("Overflow: "+ (overflowExample + 1)); //roll over effect to upper limit in underflow int underflowExample = -2147483648; System.out.println("Underflow: "+ (underflowExample - 1)); byte b = 127; // following line uncommented results in compilation error // constants are checked at compile time for size // b = b*b; double d = 1e308; System.out.println(d + "*10= " + d*10); //gradual underflow d = 1e-305 * Math.PI; System.out.print("gradual underflow: " + d + "\n "); for (int i = 0; i < 4; i++) System.out.print(" " + (d /= 100000)); } }
Overflow: -2147483648 Underflow: 2147483647 1.0E308*10= Infinity gradual underflow: 3.141592653589793E-305 3.1415926535898E-310 3.141592653E-315 3.142E-320 0.0
Comments are closed for "Java Overflow And Underflow".
this is really helpful,
thanks on the cut-to-the-chase informations.
I’m a Java developer, and I’m working
on a library for Unsigned Numbers,
.. which will be helpful for java
programmers since Java lacks support
for unsigned numbers.
Best Wishes:
khaled abdullah khunaifer
Riyadh, Saudi Arabia
Imam University, Computer Science
u are genious friend,
india…….
i love to be a gunius like you…..
be my friend
good explained
Didnt exactly get what did u mean by
‘constants are checked at compile time for size’
Does this mean
int x = 987654321123456789;
will give error???
how will over-flow come in picture now
you should mention that in case of byte, there no any problem will occurs because it produce an exception.
consider the code:
{
byte b=127;
b=b+1; //It is an exception!!
}
really helpful….
This would be clearer to see if you used the built-in Java MAX_ and MIN_ values:
Integer.MAX_VALUE, Integer.MIN_VALUE and Double.MAX_VAVUE, Double.MIN_VALUE.
This would let you avoid typing in those long hard coded values.
This really helpful. Thanks for posting such valuable information. But could you please explain a bit more in terms of like you have taken some variable of type int and float. int overflowExample = 2147483647;
System.out.println(“Overflow: “+ (overflowExample + 1)); and double d = 1e308;
so are the values you have taken for these variables greatest values for these variables. when even we add or subtract 1 these falls in overflow or underflow.
This was very helpful, Im a first year engineering student. Thank you!
new concept i learn