String 对象是否为空值判断几种方法
import java.io.File;
public class StringTest {
public static void main(String[] args) {
String str = "1111111222222222a";
if(str == null || "".equals(str))
System.out.print("str为空1\n");
if(str == null || str.length() <= 0)
System.out.print("str为空2\n");
if(str == null || str.isEmpty())
System.out.print("str为空3\n");
if(str == null || str == "")
System.out.print("str为空4\n");
long start = 0L;
long end = 0L;
start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
if(str == null || "".equals(str));
//System.out.print("str为空1\n");
}
end = System.currentTimeMillis();
System.out.println("使用equals的时间是:" + (end - start) + "毫秒!");
start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
if(str == null || str.length() <= 0);
//System.out.print("str为空1\n");
}
end = System.currentTimeMillis();
System.out.println("使用length的时间是:" + (end - start) + "毫秒!");
start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
if(str == null || str.isEmpty());
//System.out.print("str为空1\n");
}
end = System.currentTimeMillis();
System.out.println("使用isEmpty的时间是:" + (end - start) + "毫秒!");
start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
if(str == null || str == "");
//System.out.print("str为空1\n");
}
end = System.currentTimeMillis();
System.out.println("直接判断的时间是:" + (end - start) + "毫秒!");
}
}
执行结果:
使用equals的时间是:4毫秒!
使用length的时间是:0毫秒!
使用isEmpty的时间是:1毫秒!
直接判断的时间是:0毫秒!
//注意:
//1.四个方式的效率2,3,4的效率差不多,1相对慢一点,这个几种方式我试过循环1000次来比较,效率其实差不多,2,4方式最优
//2. 注意判断字符串是否为空,必须先判断null,否则用length或者equals判断会抛异常,java.lang.NullPointerException.
//3.对List、Array等进行操作时(特别是判断size、length时)先进行空引用判断
至于为啥出现效率的差异,下面附上JDK源码就非常清晰了。
/**
* Compares this string to the specified object. The result is {
@code
* true} if and only if the argument is not {
@code null} and is a {
@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {
@code String} against
*
* @return {
@code true} if the given object represents a {
@code String}
* equivalent to this string, {
@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
/**
* Returns <tt>true</tt> if, and only if, {
@link #length()} is <tt>0</tt>.
*
* @return <tt>true</tt> if {
@link #length()} is <tt>0</tt>, otherwise
* <tt>false</tt>
*
* @since 1.6
*/
public boolean isEmpty() {
return count == 0;
}
//1.isEmpty方法和length其实实现的原理是相同的,jdk 1.6版本之后建议用这个比较。
//2. equals 实现就相对复杂
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/11006.html