posted 24 years ago
I agree, the code as written looks wrong.
But the question would become interesting
if it had used == (comparison) instead of
= (assignment) in the second argument of
each line:
1 if ((s !=null) | ( i == s.length()))
2 if ((s ==null) | ( i == s.length()))
3 if ((s !=null) | | ( i == s.length()))
4 if ((s ==null) | | ( i == s.length()))
If so, that would make each of the above
lines a comparison of two boolean
expressions.
In cases 1, 2, and 3, if s is null, a
NullPointerException would
be thrown.
However, in case 4, if s
is null, the first half of the expression
would evaluate to true, then because of the
| | shortcut operator, the second half of
the expression would be skipped, which
would allow line 4 to avoid the
NullPointerException on s.length().