Java 判断字符串是否为空
2015-09-01 11:01
513 查看
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 实现就相对复杂
相关文章推荐
- struts配置Interceptor的问题
- Java 多线程 synchronized同步
- Java并发编程(二) 创建线程安全的类
- java 循环读取Map<String, String> 里面值
- java8函数式接口小例子
- CXF结合spring
- Java 验证表单工具类
- SpringMVC入门实例及详细讲解
- Java接口回调
- spring bean实例化方式
- Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
- Caused by: java.lang.ClassNotFoundException: org.jboss.logging.BasicLogger
- Hibenate错误汇总:java.lang.NoClassDefFoundError:
- Java——File
- dao、pojo、javabean
- selenium用java选中ul的最后一个li元素里内容
- Spring MVC 教程,快速入门,深入分析
- java patch补丁文件的结构
- java中IO流的知识点总结
- myeclipse中web项目没有run on server的解决办法