Bonny Mathew :
Recently I had an interview with a Software company where the following question was asked in the technical aptitude round:
Declare i in such a way that the condition is always true :
while(i != i) {
}
Is it technically possible in java to assign something of this sort??
DAle :
NaN
is not equal to itself, so
double i = Double.NaN;
But I don't think this is a good interview question.
Quote from the Java Language Specification:
NaN is unordered, so:
- The numerical comparison operators
<
,<=
,>
, and>=
returnfalse
if either or both operands areNaN
(§15.20.1).- The equality operator
==
returnsfalse
if either operand isNaN
. In particular,(x<y) == !(x>=y)
will befalse
ifx
ory
isNaN
.- The inequality operator
!=
returnstrue
if either operand isNaN
(§15.21.1). In particular,x!=x
istrue
if and only ifx
isNaN
.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=432776&siteId=1