博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA中isEmpty、null、""的区别
阅读量:4701 次
发布时间:2019-06-09

本文共 1423 字,大约阅读时间需要 4 分钟。

isEmpty()

        分配了内存空间,值为空,是绝对的空,是一种有值(值 = 空)  
 ""
        分配了内存空间,值为空字符串,是相对的空,是一种有值(值 = 空字串)  
 null
        是未分配内存空间,无值,是一种无值(值不存在)

 

得出的结论:

isEmpty()

  1.如果不分配内存空间,不能用isEmpty(),否则报空指针异常

  2.isEmpty()不能分辨出值是空还是空字符串

null

  1.null只能分辨出值是否不分配内存空间

“”

  1.不管值是否分配内存空间都不会报错

例:

public class Test {    public static void main(String[] args) {        //分配内存空间,值为空        String a = new String();        //分配内存空间,值为空字符串        String b = "";        //未分配内存空间        String c = null;        if (a != null) {            System.out.println("a值存在");        }        if (b != null) {            System.out.println("b值存在");        }        if (c == null) {            System.out.println("c值不存在");        }        if (a == "") {            System.out.println("a值存在,为空字符串");        }        if (b == "") {            System.out.println("b值存在,为空字符串");        }        //dead code        if (c == "") {            System.out.println("c值存在,为空字符串");        }        if (a.isEmpty()) {            System.out.println("a值存在,为空字符串或者为空");        }        if (b.isEmpty()) {            System.out.println("b值存在,为空字符串或者为空");        }        // Null pointer access: The variable c can only be null at this location//        if (c.isEmpty()) {//            System.out.println("String c=null");//        }    }}

结果:

1 a值存在2 b值存在3 c值不存在4 b值存在,为空字符串5 a值存在,为空字符串或者为空6 b值存在,为空字符串或者为空

 

posted on
2019-07-02 15:39 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/aaronRhythm/p/11121065.html

你可能感兴趣的文章
Java parseInt()方法
查看>>
yahoo的30条优化规则
查看>>
[CCF2015.09]题解
查看>>
[NYIST15]括号匹配(二)(区间dp)
查看>>
json_value.cpp : fatal error C1083: 无法打开编译器生成的文件:No such file or directory
查看>>
洛谷 P1101 单词方阵
查看>>
Swift DispatchQueue
查看>>
C#和JAVA 访问修饰符
查看>>
小甲鱼OD学习第1讲
查看>>
HDU-1085 Holding Bin-Laden Captive-母函数
查看>>
php提示undefined index的几种解决方法
查看>>
LRJ
查看>>
Struts2环境搭建
查看>>
Linux: Check version info
查看>>
stl学习之测试stlen,cout等的运行速度
查看>>
魔戒三曲,黑暗散去;人皇加冕,光明归来
查看>>
Error和Exception
查看>>
Python和Singleton (单件)模式[转载]
查看>>
httpclient设置proxy与proxyselector
查看>>
IT常用单词
查看>>