怎么可以“while(i == i);”在单线程应用程序中是一个非无限循环? [英] How can "while (i == i) ;" be a non-infinite loop in a single threaded application?

查看:94
本文介绍了怎么可以“while(i == i);”在单线程应用程序中是一个非无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才有一个我无法回答的问题。

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
if x NaN
(x< y)==!( x> = y)
false 如果 x y NaN

NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN. The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN. In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN.

这篇关于怎么可以“while(i == i);”在单线程应用程序中是一个非无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆