&&/|| - These are short circuit operators
This means basically that,
& and | will evaluate both the right hand side and the left hand side operands of a statement and && and || will not necessarily evaluate both. If the result is confirmed, it will be short circuited.
- For an example, lets consider the following statement;
if((x!=null) && (x.size() ==0))
if x!=null returns false we are sure that the out put is false since this is an AND operations.So if it is false the second operand will not be evaluated.So this is safe to use since the null pointer exception is avoided in this case.
- Lets consider the statement
if((x==null) || (x.size()==0))
if x==null returns true the whole result of is true since this is an OR operation. So the second operand will not be evaluated. And this is also safe to use since the null pointer is avoided.
- But following statements are not safe to use since null pointer exception can occur in run time.
if((x!=null) & (x.size() ==0))
if((x==null) | (x.size()==0))
if((x==null) && (x.size() ==0))
if((x!=null) || (x.size()==0))