When Overflow of DataTypes occur in Java

When a value more than the Maximum value of a datatype is assigned, it leads to Overflow. Java handles overflow of  datatypes on its own, for example, if we will increment 1 to Integer.MAX_VALUE, an error won’t be visible. However, the output will be Integer.MIN_VALUE.

Let us now see some examples displaying how Java handles overflow of datatypes:

Example 1

The code shows how Java handles overflow of int datatype:

Output

Example 2

The code shows how Java handles overflow of long datatype:

Output

However, this is not the case with any other datatype. Let’s see another example wherein we are multiplying the Maximum value of Float type:

Example 3

Output

In the above output, we are getting Infinity i.e. Overflow since we exceeded the maximum value of Float Datatype in Java.

Java Programming Examples
Understanding Boolean Type in Java with examples
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading