Create a JUnit test class oroperatortest:
/** <Br/> * test or ("| ") logic or operator <br/> * @ author kin <br/> */<br/> public class oroperatortest {<br/> // test "|" logic or operator <br/> @ Org. JUnit. test <br/> Public void singleor () {<br/> string STR = NULL; <br/> // single "|" <br/> If (STR = NULL | Str. equals ("") {<br/> system. out. println ("STR is null "); <br/>}</P> <p> // test the "|" logic or operator <br/> @ Org. JUnit. test <br/> Public void doubleor () {<br/> string STR = NULL; <br/> // double "|" <br/> If (STR = NULL | Str. equals ("") {<br/> system. out. println ("STR is null"); <br/>}< br/>}
When the singleor () method is run, Java is thrown. lang. nullpointerexception, indicating if (STR = NULL | Str. equals ("") Whether STR = NULL is true or not, STR on the right. equals ("") is executed. Because STR = NULL, an nullpointerexception exception is thrown;
If (STR = NULL | Str. equals ("") as long as STR = NULL is true, STR on the right. equals ("") will not be executed;
When determining whether the string is null or null, to prevent an implicit nullpointerexception caused by a single logic or "|", you can set Str. change equals ("") "". equals (STR ).