I have this below lines of code
String name = null;
if (something)
name = someString;
if (name != null && name.equals("XYZ"))
doSomethingWith ("hello");
Will the above if condition result in NullPointerException , if “something” is false ?
if not why not ?
No, it won’t. The
&&operator in Java is a short-circuit one so, ifnameisnull, thenname.equals()will not be executed (sincefalse && anythingis stillfalse).Same with
||by the way: if the left hand side evaluates totrue, the right hand side is not checked.