Java 判断字符串是否为空

来源:互联网 发布:淘宝去年的交易订单 编辑:程序博客网 时间:2024/05/21 17:18

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 实现就相对复杂
0 0
原创粉丝点击