Java Overflow And Underflow

13/10/2009

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.

Overflow and Underflow in Java int operators

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.

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 As a general rule here also Java doesn’t throw an error or exception for overflow and underflow.

Important points to remember for java overflow and underflow

  1. Overflow or underflow conditions never throw a run time exception
  2. Flowed output is predictable and reproducible. That is, its behaviour is the same every time you run the program.
Ads by Google

Example source code 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));

 }
}

Output:

Overflow: -2147483648
Underflow: 2147483647
1.0E308*10= Infinity
gradual underflow: 3.141592653589793E-305
 3.1415926535898E-310 3.141592653E-315 3.142E-320 0.0
This Core Java tutorial was posted on 13/10/2009.
Ads by Google

11 comments on “Java Overflow And Underflow

  1. 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
    3eeqeel@gmail.com

  2. i love to be a gunius like you…..
    be my friend

  3. 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

  4. 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!!
    }

  5. 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.

  6. 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.

  7. This was very helpful, Im a first year engineering student. Thank you!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>