为什么使用整型变量会引发异常? [英] Why does the use of integer variables throw an exception?
问题描述
类FloatingPoint
{
public static void main(String [] args)
{
float a = 1000f;
float b = a / 0;
System.out.println(b =+ b);
$ p $输出:b =无穷大。
如果我尝试使用int值,那么它会抛出一个运行时异常。
为什么会这样?解决方案简短的回答
b
$ b积分类型( JLS 4.2.1 )与浮点类型明显不同( JLS 4.2.3 )。在行为和操作上可能有相似之处,但也有特征性的区别,使得两者混淆可能导致许多缺陷。
只是这些差异之一。因此,简单的答案是,Java的行为是这样的,因为语言是这样说的。
积分和浮点值
整数类型的值是以下范围的整数:
字节
:从-128
到127
code> [ - 27
,2
7
-1]
-32768
至 32767
code> [ - 2 15
,2
15
-1]
-2147483648
到 2147483647
,包括 [ - 2
31
,2
31
-1]
-9223372036854775808
到 9223372036854775807
code> [ - 2 63
,2
63
-1]
char
,从'\\\ '
到'\\\'
(含),即从 0
到 65535
,即 [0,2
16
-1]
浮点类型是 float
和 double
,它们在概念上与单精度32位和双精度64位格式 IEEE 754 的值和操作。
它们的值按如下顺序排列,从最小到最大: b 此外还有一些特殊的 Not-a-Number ( $ c> x!= x 是 例如 这种情况类似于 数值运算只能抛出 它们按重要性排序,关于陷阱的常见来源。一般来说: 对于整数操作: 对于浮点操作操作: 所有浮点的一般规则操作如下: 这个已经很长的答案没有涵盖的问题,但鼓励读者浏览相关的问题和参考资料。 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. 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: The floating-point types are Their values are ordered as follows, from smallest to greatest: Additionally, there are special Not-a-Number ( In particular, For e.g. The situation is analogous with Numerical operations may only throw an They are ordered by importance, with regards to being common source for pitfalls. Generally speaking: For integer operation: For floating point operation: The general rule for all floating point operation is as follows: There are still many issues not covered by this already long answer, but readers are encouraged to browse related questions and referenced materials. 这篇关于为什么使用整型变量会引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
0.0 == -0.0
),
NaN
)值,它们是无序的。这意味着如果两个或两个操作数是 NaN
:
<
,< =
,>
,> =
返回 false
返回 false
!= / code>返回
true
true
当且仅当 x
是 NaN
。
double
,无穷和 NaN
可以被称为:
float
和 Float
。
当抛出异常时
例外
在这些情况下:
NullPointerException
,如果需要对 null
引用进行拆箱转换
ArithmeticException
如果需要进行装箱转换,则右侧为整数分隔/余数运算为零
OutOfMemoryError
内存不足
null
除以0
ArithmeticException
如果右边为零
NaN
或 0
,结果是 NaN
。
NaN
NaN
。
NaN
作为操作数的数字操作都会产生 NaN
。
$ hr
$ b $ h $附录
相关的问题
class FloatingPoint
{
public static void main(String [] args)
{
float a=1000f;
float b=a/0;
System.out.println("b=" +b);
}
}
On integral and floating point values
byte
: from -128
to 127
, inclusive, i.e. [-2
7
, 2
7
-1]
short
: from -32768
to 32767
, inclusive, i.e. [-2
15
, 2
15
-1]
int
: from -2147483648
to 2147483647
, inclusive, i.e. [-2
31
, 2
31
-1]
long
: from -9223372036854775808
to 9223372036854775807
, inclusive, i.e. [-2
63
, 2
63
-1]
char
, from '\u0000'
to '\uffff'
inclusive, that is, from 0
to 65535
, i.e. [0, 2
16
-1]
float
and double
, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations.
0.0 == -0.0
),NaN
) values, which are unordered. This means that if either (or both!) operand is NaN
:
<
, <=
, >
, and >=
return false
==
returns false
!=
returns true
x != x
is true
if and only if x
is NaN
.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)
float
and Float
.
On when exceptions may be thrown
Exception
in these cases:
NullPointerException
, if unboxing conversion of a null
reference is requiredArithmeticException
, if the right hand side is zero for integer divide/remainder operationsOutOfMemoryError
, if boxing conversion is required and there is not sufficient memory
null
On division by zero
ArithmeticException
if the right hand side is zero
NaN
or 0
, the result is NaN
.NaN
NaN
.NaN
as an operand produce NaN
as a result.
Appendix
Related questions