Overflow and Underflow in JavaIn Java, overflow can be defined as a situation when the result generated of an arithmetic operation that is performed on a numeric data type surpasses the range of that specific data type. Various numeric data types are supported by Java and they are of integer( byte, short, int, long) and floating-point numbers( float, double). Whereas Underflow in Java can be defined as a situation where the result generated of an arithmetic operation that is performed on a numeric data type is smaller than the minimum value of that data type. Let's understand in detail about both overflow and underflow in the following lines. Overflow in JavaAs discussed earlier, overflow in Java can be defined as a situation when the result generated of an arithmetic operation that is performed on a numeric data type surpasses the range of that specific data type. Let us understand in detail about overflow. 1. Integer OverflowA determined number of bits are used by Java for representing the values of integer data type. For example, 32 bits are used by Java for representing an int variable. Whereas the range for representation of signed integers in Java can be defined from Integer.MIN_VALUE till Integer.MAX_VALUE. Whenever the size of the result generated by an arithmetic operation surpasses the maximum value that can be represented (Integer.MAX_VALUE), Integer overflow occurs in Java. Filename: IntegerOverFlowExample.java Output: MAXIMUM VALUED INTEGER: 2147483647 OVERFLOW INTEGER Value: -2147483648 2. Long OverflowJust like integer variables, long variables too have a range for representation in Java which lies between Long.MIN_VALUE to Long.MAX_VALUE. Similar to Integer overflow, whenever the size of the result generated by an arithmetic operation of long variable surpasses the maximum value that can be represented (Long.MAX_VALUE), or goes below the minimum value that can be represented (Long.MIN_VALUE), Long overflow occurs in Java. Filename: LongOverflowExample.java Output: Long Overflow in Java Input the value to be added to Long variable: 12321321 Long Overflow occurred! Result cannot be Displayed. 3. Floating-point OverflowJust like integer variables, floating-point variables too have a range for representation in Java which lies between Float.MIN_VALUE to Float.MAX_VALUE. Similar to Integer overflow, whenever the size of the result generated by an arithmetic operation of floating-point variable surpasses the maximum value that can be represented (Float.MAX_VALUE), or goes below the minimum value that can be represented (Float.MIN_VALUE), Float overflow occurs in Java. Filename: FloatOverflowExample.java Output: Demonstration of Floating-point Overflow in Java Input a float value to multiply the maximum float value: 1.2651651651 Maximum Float Value: 3.4028235E38 Overflowed Float Value: Infinity Underflow in JavaIn Java, Underflow can be defined as a scenario where the result generated of an arithmetic operation that is performed on a numeric data type is smaller than the minimum value of that data type. Let us understand about types of underflows in Java. 1. Integer UnderflowAs discussed earlier, a determined number of bits are used by Java for representing the values of integer data type. For example, 32 bits are used by Java for representing an int variable. Whereas the range for representation of signed integers in Java can be defined from Integer.MIN_VALUE till Integer.MAX_VALUE. Whenever the size of the result generated by an arithmetic operation goes below the minimum value that can be represented (Integer.MIN_VALUE), Integer underflow occurs in Java. Filename: IntegerUnderflowExapmle.java Output: Minimum Integer Value: -2147483648 Underflowed Integer Value: 2147483647 Explanation: In 2's complement representation, integer underflow wraps around. 2. Long UnderflowWhenever the size of the result generated by a long arithmetic operation goes below the minimum value that can be represented (Long.MIN_VALUE), Long underflow occurs in Java. Filename: LongUnderFlowExample.java Output: Minimum Long Value: -9223372036854775808 Underflowed Long Value: 9223372036854775807 Explanation: In 2's complement representation, long underflow wraps around. 3. Floating-point UnderflowJust like integer variables, floating-point variables too have a range for representation in Java which lies between Float.MIN_VALUE to Float.MAX_VALUE. Similar to Integer underflow, whenever the size of the result generated by an arithmetic operation of floating-point variable goes below the minimum value that can be represented (Float.MIN_VALUE), Float underflow occurs in Java. Filename: FloatingPointUnderflow.java Output: Input a float value to divide the minimum float: 1.5265 Minimum Floating-point Value: 1.17549435E-38 Underflowed Value: 7.700585E-39
Next TopicPadding in Java
|