I have come across with the following two codes. Why does it not throw an exception for floating point where as in other case it will throw a runtime exception.
class FloatingPoint
{
public static void main(String [] args)
{
float a=1000f;
float b=a/0;
System.out.println("b=" +b);
}
}
OUTPUT:b=Infinity .
If I try with int values then it will throw a runtime exception. Why is it like this?
The short answer
Integral types ( JLS 4.2.1 ) are categorically different from floating point types ( JLS 4.2.3 ). There may be similarities in behavior and operations, but there are also characteristically distinguishing differences such that confusing the two can lead to many pitfalls.
The difference in behavior upon division by zero is just one of these differences. Thus, the short answer is that Java behaves this way because the language says so.
The values of the integral types are integers in the following ranges:
byte
: from -128
to 127
, inclusive, ie [-2
7
, 2
7
-1]
short
: from -32768
to 32767
, inclusive, ie [-2
15
, 2
15
-1]
int
: from -2147483648
to 2147483647
, inclusive, ie [-2
31
, 2
31
-1]
long
: from -9223372036854775808
to 9223372036854775807
, inclusive, ie [-2
63
, 2
63
-1]
char
, from '\ '
to '\'
inclusive, that is, from 0
to 65535
, ie [0, 2
16
-1]
The floating-point types are float
and double
, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations.
Their values are ordered as follows, from smallest to greatest:
0.0 == -0.0
), Additionally, there are special Not-a-Number ( NaN
) values, which are unordered . This means that if either (or both!) operand is NaN
:
<
, <=
, >
, and >=
return false
==
returns false
!=
returns true
In particular, x != x
is true
if and only if x
is NaN
.
For eg double
, the infinities and NaN
can be referred to as:
Double.POSITIVE_INFINITY
Double.NEGATIVE_INFINITY
Double.NaN
, testable with helper method boolean isNaN(double)
The situation is analogous with float
and Float
.
Numerical operations may only throw an Exception
in these cases:
NullPointerException
, if unboxing conversion of a null
reference is required ArithmeticException
, if the right hand side is zero for integer divide/remainder operations OutOfMemoryError
, if boxing conversion is required and there is not sufficient memory They are ordered by importance, with regards to being common source for pitfalls. Generally speaking:
null
For integer operation:
ArithmeticException
if the right hand side is zeroFor floating point operation:
NaN
or 0
, the result is NaN
.NaN
The general rule for all floating point operation is as follows:
NaN
.NaN
as an operand produce NaN
as a result.There are still many issues not covered by this already long answer, but readers are encouraged to browse related questions and referenced materials.
Because floats actually have a representation for the "number" you're trying to calculate. So it uses that. An integer has no such representation.
Java (mostly) follows IEEE754 for its floating point support, see here for more details.
It is because integer arithmetic always wraps it's result except for the case of (Division/Remainder By Zero).
In case of float, when there is an overflow or underflow, the wrapping goes to 0, infinity or NaN.
During the overflow, it gives infinity and during underflow, it gives 0.
Again there are positive & negative overflow/underflow.
Try:
float a = -1000;
float b = a/0;
System.out.println("b=" +b);
This gives a negative overflow
Output
b=-Infinity
Similarly positive underflow will result in 0 and negative underflow in -0.
Certain operations can also result in returning a NaN(Not a Number) by float/double.
For eg:
float a = -1000;
double b = Math.sqrt(a);
System.out.println("b=" +b);
Output
b=NaN
It's a programming and math standard for representing / by zero values. float has support for representing such values in JAVA. int (integer) data type doesn't have way to represent same in JAVA.
Check :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.