Java Basics : Exploring Overflow and Underflow Concepts

Ritu Sitlani
Javarevisited
Published in
4 min readMar 3, 2024

Introduction:

Java supports various primitive numeric data types like byte, short, int, and long, each with its own range of representable values.

byte:

  • Size: 8 bits
  • Range: -128 to 127

short:

  • Size: 16 bits
  • Range: -32,768 to 32,767

int:

  • Size: 32 bits
  • Range: -2,147,483,648 to 2,147,483,647

long:

  • Size: 64 bits
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float:

  • Size: 32 bits
  • Range: Approximately ±3.40282347E+38F (6–7 significant decimal digits)

double:

  • Size: 64 bits
  • Range: Approximately ±1.79769313486231570E+308 (15 significant decimal digits)

char:

  • Size: 16 bits
  • Range: 0 to 65,535 (unsigned)

boolean:

  • Represents true or false, typically stored as 1 bit.

These ranges set the highest and lowest values each data type can hold. It’s crucial to remember these limits while handling various data types in Java. Going beyond these limits may cause overflow or underflow, leading to errors in the code.

Java Overflow

In Java, overflow occurs when the result of an arithmetic operation on numeric data types exceeds the maximum value that the data type can represent.

Below is an example showcasing max value accepted in Java with the int data type:


public class overFlow {
public static void main(String[] args) {

//Print Max value for int
int maxValue = Integer.MAX_VALUE;
System.out.println(maxValue);

}
}

Output :

2147483647

Overflow Example :

Below is a simple example showcasing overflow in Java with the int data type:


public class overFlow {
public static void main(String[] args) {

//Declaring variables
int maxValue = Integer.MAX_VALUE;
System.out.println(maxValue + 1);

}
}
-2147483648

When the maximum value for an int in Java is reached, adding 1 to it exceeds the maximum representable value, triggering an overflow. In Java, overflow wraps around, effectively causing the value to cycle back to the minimum representable value for an int, which is -2,147,483,648.

Java does not throw an exception when an overflow occurs; that is why it can be hard to find errors resulting from an overflow.

Java Underflow

In Java, underflow typically happens when the result of an arithmetic operation on numeric data types falls below the minimum value that the data type can represent. It indicates a situation where the computed result is smaller than the minimum representable value for the given data type.

Below is an example showcasing min value accepted in Java with the int data type:


public class underFlow {
public static void main(String[] args) {

//Declaring variables
int minValue = Integer.MIN_VALUE;
System.out.println(minValue);
}
}
-2147483648

Underflow Example :

Below is a simple example showcasing underflow in Java with the int data type:


public class underFlow {
public static void main(String[] args) {

//Declaring variables
int minValue = Integer.MIN_VALUE;
System.out.println(minValue - 1);

}}
2147483647

Let’s clarify :

If you directly assign a literal value like 2147483648 to an int variable, Java will give you a compilation error because the value is out of the range of the int data type. The maximum value an int can hold is 2147483647.

Similarly, The minimum value an int can hold is -2147483647.

It’s worth noting

Both overflow and underflow are consequences of arithmetic operations, where numbers are combined, manipulated, and processed in expressions. They’re not inherent properties of individual numbers but rather the outcomes of calculations involving those numbers.

An added perspective

To enhance readability, the underscore can be used in the preceding example.

* cannot use an underscore at the start or end of numeric literal

How to avoid overflow/underflow?

It’s crucial to choose the appropriate data type based on the range of values you expect your variable to hold. For instance, if you’re working with small numbers, you can use byte or short. For larger numbers, int is typically sufficient, and for very large numbers, or when dealing with operations that might result in overflow, long is the preferred choice.

Summary

Overflow or Underflow can lead to unexpected behavior and incorrect results in programs if not handled properly. It’s essential to understand the range of values for each data type and ensure that arithmetic operations do not result in overflow or underflow when designing Java programs.

Thankyou & happy Coding ! 😊

--

--

Ritu Sitlani
Javarevisited

Hi, I'm an Engineer sharing insights on software testing and Java - Learning along the way!