1. The methods to determine that a string str is not empty are:
1. str == null;
2. "".equals(str);
3. str.length <= 0;
4. str.isEmpty();
Note: length is an attribute, which is a property owned by a collection class object, and obtains the size of the collection.
For example: array.length is to get the length of the array.
length() is a method. Generally, string class objects have this method, which also obtains the string length.
For example: string.length();
illustrate:
1. Null means that this string does not point to anything. If you call its method at this time, a null pointer exception will appear.
2. "" means that it points to a string of length 0, and it is safe to call its method at this time.
3. null is not an object, "" is an object, so null has no space allocated, "" allocates space, for example:
String str1 = null; str reference is empty
String str2 = ""; str refers to an empty string
str1 is not an instantiated object yet, while str2 has been instantiated.
Objects are compared with equals, null is compared with equal sign.
If str1=null; the following is wrong:
if(str1.equals("")||str1==null){ }
The correct way to write it is if(str1==null||str1.equals("")){ //So when judging whether a string is empty, first determine whether it is an object. If so, then determine whether it is an empty string}
4. Therefore, to determine whether a string is empty, you must first make sure it is not null, and then judge its length.
String str = xxx;
if(str != null && str.length() != 0) { }
2. The following are four methods for java to determine whether a string is empty:
The efficiency of the four methods are as follows:
JudgeString1 time taken: 625ms
JudgeString2 time taken: 125ms
JudgeString3 time taken: 234ms
JudgeString4 time taken: 109ms
The code copy is as follows:
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-1-16
* Time: 10:43 am
* The efficiency of determining whether a string is empty
*/
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);
}
/**
* Method 1: The method used by most people is intuitive, convenient, but very inefficient;
* Method 2: Comparing string lengths is the best method;
* Method 3: The method provided by Java SE 6.0 has only begun to provide, the efficiency and method 2 are almost equal, but for compatibility reasons, it is recommended to use method 2;
* Method 4: This is a relatively intuitive and simple method, and the efficiency is also very high, which is similar to the efficiency of methods 2 and 3;
*/
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-consuming: " + (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-consuming: " + (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-consuming: " + (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-consuming: " + (endTime - startTiem) + "ms");
}
}