java - NullPointerException on Null Check -
i messed below code cannot able distinguish between 2 code snippets:
string check=null; if(check!=null && check.isempty()){ system.out.println("get inside"); }
above code works fine , print message.
if(check==null && check.isempty()){ system.out.println("get inside") }
this code throw nullpointerexception.not able distinguish between code please help.
the &&
operator short-circuit, doesn't evaluate right hand side if it's not necessary. in case, it's necessary:
if(check==null && check.isempty()){ system.out.println("get inside") }
if check
null
(and that's case), you'll continue check condition after &&
, , check.isempty()
. since check
null
, it's null.isempty()
nep.
why?
when 2 expressions have &&
between them, if first 1 false, answer false , other side won't checked, when first true
, have continue , check other side know if expression should evaluated true
or false
.
but when have:
if(check!=null && check.isempty()){ system.out.println("get inside") }
then right side won't reached, since it's redundant check it. got false
, , doesn't matter if true
or false
on right side, result false
since and
(false && whatever = false). why checking?
this link might you.
(i recommend follow java naming conventions , make variables begin lowercase).
Comments
Post a Comment