Java analysis of the problem of determining whether a String is empty

  • 2020-04-01 02:48:11
  • OfStack

One, to determine a string STR is not null method:

1. STR == null;
2, "" equals (STR);
3, STR. Length < = 0;
4, STR. IsEmpty ();
Note: length is a property that is normally owned by a collection class object and gets the size of the collection.
                      For example, array.length is the length of an array.
                  Length () is the method that most string class objects have to get the length of the string.
                      For example: string.length();
Description:
  1. Null means that the string does not point to anything, and if you call its methods, a null-pointer exception will occur.
  2. "" means it points to a string of length 0, and the method is safe to call it.
  3. Null is not an object, "" is an object, so null does not allocate space, "" allocated space, for example:
      Strings str1 = null; The STR reference is null
      String str2 = ""; STR refers to an empty string
      Str1 is not yet an instantiated object, while str2 is already instantiated.
      Objects are compared with equals and null with equals.
      If the str1 = null; The following writing errors:
            If (str1. Equals (" ") | | str1 = = null) {}
    The correct way to write this is if(str1==null|, |, str1.equals("")){//
  4. Therefore, to determine whether a string is null, first make sure it is not null, and then determine its length.
        String STR = XXX;
        If (STR! = null && STR. Length ()! = 0) {}

2. The following are four Java methods for determining whether a string is empty:

The efficiency of the four methods is as follows:

JudgeString1 time: 625ms
JudgeString2 time: 125ms
JudgeString3 takes time: 234ms
JudgeString4 takes time: 109ms



public class JudgeStringIsEmptyOrNot {
    public static void main(String[] args) {
        JudgeString1("w_basketboy", 10000);
        JudgeString2("w_basketboy", 10000);
        JudgeString3("w_basketboy", 10000);
        JudgeString4("w_basketboy", 10000);
    }
    
    public static void JudgeString1(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || "".equals(str)) {
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function1 Time: " + (endTime - startTiem) + "ms");
    }
    public static void JudgeString2(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || str.length() <= 0) {
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function4 Time: " + (endTime - startTiem) + "ms");
    }
    public static void JudgeString3(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || str.isEmpty()) {
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function3 Time: " + (endTime - startTiem) + "ms");
    }
    public static void JudgeString4(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || str == "") {
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function4 Time: " + (endTime - startTiem) + "ms");
    }
}


Related articles: