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>=returnfalseif either or both operands areNaN(§15.20.1).- The equality operator
==returnsfalseif either operand isNaN. In particular,(x<y) == !(x>=y)will befalseifxoryisNaN.- The inequality operator
!=returnstrueif either operand isNaN(§15.21.1). In particular,x!=xistrueif and only ifxisNaN.
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