Java determines that a string str is NOT null: method and Time efficiency

Source: Internet
Author: User
Tags java se

The method to determine if a string str is not empty is:

1, str = = NULL;

2, "". Equals (str);

3, str.length <= 0;

4, Str.isempty ();

Note: Length is the property that the generic collection class object owns and gets the size of the collection.

For example: arrays. Length is how long the array is obtained.

Length () is a method in which a generic string class object has the method and also gets the string length.

For example: String. Length ();

String str = NULL indicates that the string does not point to anything, and if this time calls his method, a null pointer exception will appear

String str = "" means that it only wants a string of length 0, when calling his method is safe

Null is not an object, and "" is an object, so null is not allocated space, "" is allocated space

String str1 = null; The str reference is empty

String str2 = ""; str references an empty string

STR1 is not an instantiated object, STR2 has been instantiated

objects are compared with equals, and null is compared by an equal sign.

If str1=null, the following syntax error:

if (Str1.equals ("") | | Str1==null) {}

The correct wording is if (str1==null| | Str1.equals ("")) {//So when judging whether a string is empty, first determine if it is not an object, and if so, then determine if it is an empty string}

So, to determine whether a string is empty, first make sure that he is not null, and then judge his length.

String str = XXX;

if (str! = null && str.length ()! = 0) {}

The first method is the least efficient, is also a method used by many people, intuitive and convenient

The second method compares the length of the string to a high efficiency and is the best method

The third method is that Java SE 6.0 only begins to provide methods that are nearly equal in efficiency and method two, but for compatibility, it is recommended to use the method two

The fourth method is a more intuitive and simple method, and the law is also very high, and two or three efficiency is similar to

Java determines that a string str is NOT null: method and Time efficiency

Related Article

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.