Java | int and float overflow and underflow with examples

|
| By Webner

First we should know what is overflow and underflow.

Overflow is a condition where value crosses the maximum prescribed size of a data type.
Underflow is a condition where value reaches the minimum prescribed size of a data type.

With Overflow and Underflow condition, either the program crashes, or an error occurs or Language Implementation handles this condition in its own way.

Lets see examples of overflow and underflow of integer and float and let’s see how Java handles overflow and underflow of data types.

Overflow and Underflow of Integer in Java

The default size for Integer is 4 bytes (32 bits). The value range allowed by integer in Java is 231 to 231-1 (-2147483658 to 2147483657).

Suppose we have following class:

public class OverflowUndeflowProgram {
    public static void main(String[] args) {
        int varInt = 2147483647; // Maximum prescribed value for Integer data
        System.out.println(varInt);
        int varInt2 = -2147483648; // Minimum prescribed value for Integer data
        System.out.println(varInt2);
    }
}

Above program will execute successfully.

Now consider these cases.

Case 1 – Assigning a value out of range to a variable

int varInt = 2147483648; // larger value than maximum allowed value by Int
System.out.println(varInt);

This will generate error. Error will be displayed as follows:

OverflowUndeflowProgram.java:4:
error: integer number too large: 2147483648
int intVar = 2147483648;
^
1 error

If we go below the minimum limit value of integer as shown below:

int varInt2 = -2147483649; //less than minimum allowed value by Int.
System.out.println(varInt2);
Then also same error will generated as shown for intVar.

Case 2 – Incrementing a variable to take it out of range

If we write program as follows:

int intVar = 2147483647;
System.out.println(intVar+1); // Adding 1 to Maximum value of data type causing overflow

int intVar2 = -2147483648;
System.out.println(intVar2 – 1); // Subtracting 1 from Minimum value of data type causing
underflow.

The above program works and gives following output:

-2147483648 // minimum allowed value
2147483647 // maximum allowed value

From the above output, it is easily understood that value starts from minimum prescribed value for overflow and continues from there. Value starts from maximum for underflow.

Case 3 – Multiplying min/max values

Overflow examples:
int intVar = 2147483647; //output is shown in comment against each statement
System.out.println(intVar*2); // -2
System.out.println(intVar*3); // 2147483645
System.out.println(intVar*4); // -4
System.out.println(intVar*5); // 2147483643

System.out.println(intVar* -2); // 2
System.out.println(intVar* -3); // -2147483645

Underflow examples:
int intVar2 = -2147483648;
System.out.println(intVar2 * 2); // 0
System.out.println(intVar2 * 3); // – 2147483648
System.out.println(intVar2 * 4); // 0
System.out.println(intVar2 * 5); // – 2147483648

The above output of multiplication of variables by integers causing overflow and underflow respectively.

From the above output, we can see that when we multiply positive largest value by even positive integer, then it results in negative of lowest value. When we multiply positive largest value by odd positive integer, then it results in negative value of (MAXIMUM – (oddPositiveVal -1));

When we multiply LOWEST integer by any even value (positive or negative), then it gives 0 always, and when we multiply LOWEST integer by any odd value (positive or negative) , then it prints LOWEST prescribed value always.

Overflow and Underflow of Float in Java

There are two ways to represent float variables in Java: float and double.

Float data types take 4 bytes of memory space and double data types take 8 bytes of memory space. Float value assigned to any variable should be appended with ‘f’ or ‘F’ in Java. Without appending ‘f’ or ‘F’ to float value, Java throws an error. Double value assigned to any variable can be appended with ‘d’ or ‘D’, but it is optional.

Following is the example to declare float and double variables:

float floatVar = 2.55f;
double doubleVar = 2.55;

Overflow of float in Java results in Infinity and Underflow results in value 0.0 .

Lets see example :

public class OverflowUndeflowProgram {

    public static void main(String[] args) {
        float floatVar = 3.4028235E38 f;
        double doubleVar = 1e308;
        System.out.println(floatVar);
        System.out.println(doubleVar);
    }
}

Above example gives following output:
3.4028235E38
1.0E308

If we write as follows:

float floatVar = 3.4028235E38f;
double doubleVar = 3.142E-320;//1e308;
System.out.println(floatVar * 20f); // causing overflow
System.out.println(doubleVar/100000); // causing underflow

Following output is produced:
Infinity
0.0

Leave a Reply

Your email address will not be published. Required fields are marked *