Java, "" != null ; new int[] {}!= null

版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/85253446

这两天吃亏了,特总结如下:

String s="";

s==null的结果是false,s.length的值是0,只用if(s==null){...}根本无法进入if中

        String s="";
        System.out.println(s==null);//false
        System.out.println(s.length());//0

这只是简单展示出来,当前端的请求参数中如果有【key1: 】,则只用if(key1==null){...}这个条件是会返回false,if中的代码不执行!使用StringUtils.isBlank()能很好的判断

2

        int[] arr = new int[]{};
        System.out.println(arr==null);//false
        System.out.println(arr.length==1);//false
        System.out.println(arr.length==0);//true

当然

        List<Integer> list = new ArrayList<>();
        System.out.println(list==null);//false
        System.out.println(list.size()==1);//false
        System.out.println(list.size()==0);//true

谨记 以上!!

猜你喜欢

转载自blog.csdn.net/u010002184/article/details/85253446