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??
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With