refresh java logo

Overflow/Underflow and Widening/Narrowing

This tutorial explains some of the important concepts related with variables and data types like Overflow, Underflow, Widening, Narrowing etc. These are some useful concepts that a programmer should be aware of.

Overflow and Underflow in java

Overflow and Underflow in java is a condition when an operation returns a result that crosses the range of data type. If the value crosses the maximum prescribed size of a data type, it is called as Overflow and if the value crosses the minimum prescribed size, it is called as Underflow. Java does not throw any error or exception in any of the above cases.

For example, an int is of 32 bit in Java, any value that crosses 32 bits gets rolled over, which means after incrementing 1 on max value of int(2147483647), the returned value will be -2147483648 which is minimum value of int . Similarly after decrementing 1 from -2147483648, the result will be 2147483647 which is maximum value of int.

For Floating point data types, overflow will result in infinity while underflow results in 0.0

 class OverflowAndUnderflow
  {
     public static void main(String args[])
      {
        int intOverflow = 2147483647 + 1;
        System.out.println("int overflowed value = "+ intOverflow);
 
        int intUnderflow = -2147483648 - 1;
        System.out.println("int underflowed value =  "+ intUnderflow);
        // Compilation error in below code, Constant values are checked at compile time for size limit
        // int overflow = 2147483648;  // Crossed maximum prescribed value for int data type
        double doubleOverflow = 1e308 * 10;
        System.out.println("double overflowed value = " + doubleOverflow);
 
        double doubleUnderflow = 4.9e-324 / 100000;
        System.out.println("double underflowed value = " + doubleUnderflow);
       }
  }	

Output:

int overflowed value = -2147483648
int underflowed value = 2147483647
double overflowed value = Infinity
double underflowed value = 0.0

Widening and Narrowing in java

Widening is a process in which a smaller type is converted to wider/larger type. This is an implicit/automatic conversion, Java itself does this for us. This is also known as Upcasting. Automatic conversion of a subclass reference variable to a superclass reference variable is also part of Widening.

 Byte → Short → Int → Long → Float → Double
            Subclass → Superclass 
       Widening or Implicit Conversion 	

Narrowing is a process in which a larger type is converted to smaller type. This needs to be done explicitly by the programmer. This is also known as Downcasting. Explicit conversion of a superclass reference variable to a subclass reference variable is also part of Narrowing.

 Double → Float → Long → Int → Short → Byte
            Superclass → Subclass  
        Narrowing or Explicit Conversion 	
 class WideningAndNarrowing
   {
      public static void main(String [] args)
         {
           byte b=20;
           int i=b;    //byte to int widening
           long l=b;   //byte to long widening
           double d=b; //byte to double widening
           System.out.println("int value after widening : "+ i);
           System.out.println("long value after widening : "+ l);
           System.out.println("double value after widening : "+ d);

           double d2 =20.5;
           byte b2 = (byte)d2;   //Narrowing double to byte
           // long ll=d2;  //compile time error, must be explicitly casted 
           long l2= (long)d2;    //Narrowing double to long
           float f2= (float)d2;  //Narrowing double to float  
           System.out.println("Narrowing double value to byte : "+ b2);
           System.out.println("Narrowing double value to long : "+ l2);
           System.out.println("Narrowing double value to float : "+ f2);
         }
   }   	

Output:

int value after widening : 20
long value after widening : 20
double value after widening : 20.0
Narrowing double value to byte : 20
Narrowing double value to long : 20
Narrowing double value to float : 20.5

Does overflow and underflow happens with Non Primitive data types as well ?

No, as it doesn't contain value, it contains only reference.

★★★
  • Every time you run Overflow or underflow program, it will return same result.
  • Every programming language has it's own way of handling overflow and underflow conditions.
  • Overflow and Underflow deals on min/max values of data types while Widening and Narrowing deals on conversion of data type.