Java method to judge whether a string is null

Source: Internet
Author: User

Java method to judge whether a string is null
Today, I wrote code and found my code: if (! Username. equals ("")&&! Userpassword. isEmpty ()&&! PhString. isEmpty ()&&! Userpassword2.isEmpty () as above: if there is nothing in the phString here, then phString. the isEmpty () method returns a null pointer exception when phString = "51cto";, phString. the return value of isEmpty () is false. That is to say, the return value of this method is boolean. If length () is 0, true is returned. Otherwise, false is returned. however, this method is not compatible and prone to errors. The following lists several common null judgment methods: Method 1: a method that is used by a maximum of people, intuitive, convenient, but inefficient. method 2: Compare the string length with high efficiency. It is the best method I know. method 3: the method provided by Java SE 6.0 is almost the same as method 2. For compatibility reasons, method 2 is recommended. 1: if (s = null | s. equals (""); 2: if (s = null | s. length () <= 0); 3: if (s = null | s. isEmpty ());

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.