java - Checking all conditions in a logical disjunction -
i found question in line of code. ignore fact code not make sense:
if (object != null || object.somemethod()) object.dosomething(); first thinking code throw nullpointerexception if object null. but, logical disjunction , if 1 of conditions true whole condition true. compiler doesn't checks second condition , doesn't throws nullpointerexception.
is java standard behavior or implementation specific? if second case true code not secure.
when object not null, short-circuit evaluation occurs, , object.somemethod() never called.
when object null, expression should throw nullpointerexception when object.somemethod() evaluated.
this not particularly useful condition. consider whether && intended instead of ||.
Comments
Post a Comment