Difference between a single operator and two logical operators or the "|" Operator

Source: Internet
Author: User

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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.