Java Reference
In-Depth Information
< Day Day Up >
Puzzle 30: Son of Looper
Provide a declaration for i that turns this loop into an infinite loop:
while (i != i + 0) {
}
Unlike previous loopers, you must not use floating-point in your answer. In other words, you must
not declare i to be of type double or float .
Solution 30: Son of Looper
Like the previous puzzle, this one seems impossible at first glance. After all, a number is always
equal to itself plus 0, and you were forbidden from using floating-point, so you can't use NaN.
There is no NaN equivalent for the integral types. What gives?
The inescapable conclusion is that the type of i must be non-numeric, and therein lies the solution.
The only non-numeric type for which the + operator is defined is String . The + operator is
overloaded : For the String type, it performs not addition but string concatenation. If one operand in
the concatenation is of some type other than String , that operand is converted to a string prior to
concatenation [JLS 15.18.1].
In fact, i can be initialized to any value so long as it is of type String ; for example:
 
 
Search WWH ::




Custom Search