Please help me how does the string.equals in java work with null value? Is there some problem with exceptions? Three cases:
boolean result1,result2, result3;
That piece of code will throw a NullPointerException
whenever string1 is null and you invoke equals
on it, as is the case when a method is implicitly invoked on any null object.
To check if a string is null, use ==
rather than equals
.
Although result1
and result3
will not be set due to NullPointerExceptions, result2 would be false (if you ran it outside the context of the other results).