I have the following conditional statement in Java and would appreciate some insight on the purpose of using “not” (!) on a method in the conditional in such a manner:
char[] buf = new char[10];
BufferedReader in = new BufferedReader(r);
if (buf[0] != '<') {
in.reset();
if (!this.handleContent(in)) {
return;
}
}
Revising or simplifying it to the following appears to achieve the same result:
if (buf[0] != '<') {
in.reset();
handleContent(in);
}
Thanks!!!
3
handleContent(in)
is an expression of type boolean
, because the handleContent
method returns a boolean
.
That is, if handleContent(in)
is true
, then !handleContent(in)
is false
.
For the code you have shown, there is no difference between
if (!handleContent(in)) {
return;
}
and
handleContent(in);
because there is no statement after that conditional: it doesn’t matter whether you return or not.
However, I suspect there actually is something after that conditional that you’re not showing us, which may or may not have some side effect.