Freenet compilation errors.
Per Bothner
per@bothner.com
Sat Jan 6 22:42:00 GMT 2001
"Mark J. Roberts" <mjr@statesmean.com> writes:
> private boolean isGreater(Key k) {
> for (int i = 0 ; true ; i++) {
> if (i == k.val.length)
> return false; // k is equal or smaller
> else if (i == val.length)
> return true;
> else if (val[i] < k.val[i])
> return true;
> else if (val[i] > k.val[i])
> return false;
> }
> }
>> gcj complains that there is no return statement in isGreater. Sun's javac
> doesn't mind because eventually i will equal val.length.
That has nothing to do with it - neither compiler knows that
i will equal val.length, and javac will be just as happy to
compile an infinite loop.
The point is that the loop can never terminate normally
because of the "true" In fact there is a special case in
the rules for "unreachable statements" for "true". If you write:
boolean t = true;
for (int i = 0 ; t ; i++) {
if (i == k.val.length)
return false; // k is equal or smaller
else if (i == val.length)
return true;
else if (val[i] < k.val[i])
return true;
else if (val[i] > k.val[i])
return false;
}
then javac will complain.
--
--Per Bothner
per@bothner.com http://www.bothner.com/~per/
More information about the Java
mailing list