One, to judge a string STR is not NULL method is:
1, str = NULL;
2, "". Equals (str);
3, str.length <= 0;
4, Str.isempty ();
Note: Length is an attribute that the General collection class object owns and gets the size of the collection.
For example, the array. Length is the length that gets the array.
Length () is a method in which the generic string class object has the method and also gets the length of the string.
For example: String. Length ();
Description
1, null means that this string does not point to anything, if you call its method at this time, then there will be a null pointer exception.
2, "" means that it points to a string of length 0, this time the method of calling it is safe.
3., null is not an object, "" is an object, so null does not allocate space, "" allocated space, for example:
String str1 = null; STR reference is empty
String str2 = ""; str references an empty string
STR1 is not yet an instantiated object, and STR2 has been instantiated.
objects are compared with equals, and null is compared with equals.
If Str1=null, the following is the wrong spelling:
if (Str1.equals ("") | | Str1==null) {}
The correct notation is if (str1==null| | Str1.equals ("")) {//So in determining whether the string is empty, first judge is not an object, if yes, then judge whether the empty string}
4. Therefore, 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) {}
Two, the following is the Java judgement string is empty four ways:
The efficiency of the four methods is as follows:
JudgeString1 time consuming: 625ms
JudgeString2 time consuming: 125ms
JudgeString3 time consuming: 234ms
JudgeString4 time consuming: 109ms
Copy Code code as follows:
/**
* Created with IntelliJ idea.
* User:administrator
* date:14-1-16
* Time: 10:43
* Efficiency issues that determine 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 One: Most people use a method, intuitive, convenient, but the efficiency is very low;
* Method Two: Compare string length, high efficiency, is the best method;
* Method Three: Java SE 6.0 only began to provide the method, efficiency and method two almost equal, but for compatibility considerations, recommended use of method two;
* Method Four: This is a more intuitive, simple method, but also very high efficiency, and method two or three of the efficiency of almost;
*/
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");
}
}