this is because in line:: if(str == null | str.length() == 0)--->you are using '|' operator in which both the operands get evaluted and since str is assigned to null and using '.' operator on null always throws a null pointer exception as:: str.length() is equivalent to null.length() use '||' in place of '|' and there will be no exception.

"History would be kind to me, for I intend to write it."

Here is more on logical[shorty circuit] and bitwise operators

[Bold]Logical && {Short circuit}[/Bold]

Here if [Bold] expr1 results in false [/Bold] then the result of the complete expre expr1 && expr2 will always be false, So JVM will not compute expr2.

[Bold]Logical OR {Short circuit}[/Bold]

Here if [Bold] expr1 results in true [/Bold] then the result of the complete expre expr1 || expr2 will always be true, So JVM will not compute expr2.

[Bold]Bitwise And {Non short-circuit][/Bold]

Here if [Bold] expr1 results in false [/Bold] even though the result of the expression expr1 & expr2 will always result in false, JVM will still go ahead and evaluate expr2.

[Bold]Bitwise Or {Non short-circuit][/Bold]

Here if [Bold] expr1 results in true [/Bold] even though the result of the expression expr1 | expr2 will always result in true, JVM will still go ahead and evaluate expr2.

Hope this clears everything. Thanks Deepak

SCJP, SCWCD, SCBCD

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637

posted

0

Here is more on logical[shorty circuit] and bitwise operators

Logical && {Short circuit}

Here if expr1 results in false then the result of the complete expre expr1 && expr2 will always be false, So JVM will not compute expr2.

Logical OR {Short circuit}

Here if expr1 results in true then the result of the complete expre expr1 || expr2 will always be true, So JVM will not compute expr2.

Bitwise And {Non short-circuit]

Here if expr1 results in false even though the result of the expression expr1 & expr2 will always result in false, JVM will still go ahead and evaluate expr2.

Bitwise Or {Non short-circuit]

Here if expr1 results in true even though the result of the expression expr1 | expr2 will always result in true, JVM will still go ahead and evaluate expr2.