Overflow of DataTypes in Java


Overflow occurs when the given value is more than the maximum prescribed size of a data type. The overflow condition can result to an error or now the implementation of the programming language handles it on its own.

To display overflow of datatypes, I have taken an example of float datatype. Float data type is a single-precision 32-bit IEEE 754 floating point.

The range of a float datatype is −

approximately ±3.40282347E+38F

The following program display overflow of datatypes in Java.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      System.out.println("Displaying Overflow... ");
      float val1 = 3.3976835E38f;
      System.out.println(val1 * 25f);
   }
}

Output

Displaying Overflow...
Infinity

In the above program, the float variable is initialized to.

float val1 = 3.3976835E38f;

After that, multiplication operation is performed on it to check for overflow.

val1 * 25f;

Since it extends the maximum range, “Infinity” is returned as the output.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

754 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements