怎么可以“while(i == i);”在单线程应用程序中是一个非无限循环? [英] How can "while (i == i) ;" be a non-infinite loop in a single threaded application?
问题描述
我刚才有一个我无法回答的问题。
I just got a question that I can't answer.
假设你在Java中有这个循环定义:
Suppose you have this loop definition in Java:
while (i == i) ;
i
的类型是什么?值 i
如果循环不是无限循环且程序只使用一个线程?
What is the type of i
and the value of i
if the loop is not an infinite loop and the program is using only one thread?
推荐答案
double i = Double.NaN;
Double.equals()拼出答案:Double.NaN == Double。 NaN的值为false。这在Java语言规范中详细阐述浮动 - 点类型,格式和值:
The API for Double.equals() spells out the answer: "Double.NaN==Double.NaN has the value false". This is elaborated in the Java Language Specification under "Floating-Point Types, Formats, and Values":
NaN
是无序的,所以数字
比较运算符<
,< =
,>
,> =
返回false
如果是两个
操作数都是NaN
。
等于运算符==
返回false
如果
,则操作数为NaN
,
不等运算符!=
返回true
如果
操作数是NaN
。 特别是
,x!= x
是true
if和only
ifx
是NaN
,(x< y)==!( x> = y)
将false
如果x
或y
是NaN
。
NaN
is unordered, so the numerical comparison operators<
,<=
,>
, and>=
returnfalse
if either or both operands areNaN
. The equality operator==
returnsfalse
if either operand isNaN
, and the inequality operator!=
returnstrue
if either operand isNaN
. In particular,x!=x
istrue
if and only ifx
isNaN
, and(x<y) == !(x>=y)
will befalse
ifx
ory
isNaN
.
这篇关于怎么可以“while(i == i);”在单线程应用程序中是一个非无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!