当 arr 是一个array时,写Java开始的corner case常常会写类似下面的语句:

if(arr == null || arr.length == 0){
return 0;
}

其实这是两个条件, arr==null 和 arr.length==0 是不同的:

arr.length == 0说明 arr is an empty array, which means an empty array is assigned to arr; arr.length would yields 0.

arr == null means that null is assigned to arr, arr.length will throw out NullPointerException. 

所以在写这两个条件时必须保证arr==null在前

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java: arr==null vs arr.length==0的更多相关文章

  1. 数组中array==null和array.length==0的区别

    //代码public class Test1 { public static void main(String[] args) { int[] a1 = new int[0]; int[] a2 = ...

  2. java——arr == null || arr.length == 0

    这两者是不同的: arr == null; int[] arr = null; arr.length == 0; int[] arr =new int[0];

  3. protobuf接口调用报错:java.nio.charset.MalformedInputException: Input length = 1

    使用protobuf定义的接口api发起http请求报错,日志如下: [-- ::] DEBUG AbstractPool: - server updated, node=, server={ nod ...

  4. 【Java】 剑指offer(53-2) 0到n-1中缺失的数字

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个 ...

  5. Java字符串null相加

    Java字符串null相加 最近和同事讨论了下面的一段代码: String a = null; a += a; System.out.println(a); 运行结果: nullnull 本着学习的态 ...

  6. org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1

    项目启动报错2018-12-21 14:06:24.917 INFO 23472 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refr ...

  7. Java 获取客户端ip返回127.0.0.1问题

    Java开发中使用 request.getRemoteAddr 获取客户端 ip ,返回结果始终为127.0.0.1.原因是服务器使用了nginx反向代理. 解决办法:在nginx配置文件nginx. ...

  8. java中NULL与" "的区别

    null是没有地址""是有地址但是里面的内容是空的 null和""的区别 问题一: null和""的区别 String s=null; st ...

  9. java中null和""的区别

    问题一: null和""的区别 String s=null; s.trim()就会抛出为空的exception String s=""; s.trim()就不会 ...

随机推荐

  1. OpenCV count the number of connected camera 检测连接的摄像头的数量

    有时候在项目中我们需要检测当前连接在机子上的摄像头的数量,可以通过下面的代码实现,其中连接摄像头的最大数量maxCamNum可以任意修改: /** * Count current camera num ...

  2. VS2012网布网站与IIS配置

    一 VS发布 1首先是 点击自己创建的 web项目 ,右键发布 2  在之后出现的界面中选择配置文件 , 选择或导入发布配置文件,下拉框,选择新建 跳转到下一步 3 在连接界面中 在发布方法中选择文件 ...

  3. htc M8 无法自动恢复数据连接(4g)的问题解决

    情况如下:htc m8 tdd-lte的双待手机,4g.2g同时在线. 本月出现,在短时间没有信号的情况后,无法恢复数据连接,哪怕是edge,更不论4g了. 尝试各种方法无解.最后咨询10086解决此 ...

  4. extjs Combobox动态加载数据问题,mode:local 还是remote

    var fabircTypeDs = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({ url: 'province.do' }), reade ...

  5. 在Windows2003上安装Active Directory Management Gateway Service

    为了让基于Windows2003的域控能够被Powershell管理,必须安装KB968934,但是直接安装会报以下的错误,必须先安装NDP35SP1-KB969166.但是"因为基于 Wi ...

  6. SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)

    Boring Counting Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descriptio ...

  7. Yii源码阅读笔记(十八)

    View中的查找视图文件方法和渲染文件方法 /** * Finds the view file based on the given view name. * 通过view文件名查找view文件 * ...

  8. java 重写 重载

    首先我们来讲讲:重载(Overloading) (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态 ...

  9. {转}每次从vss获取文件都是只读

    http://www.cnblogs.com/lauplay/p/3141636.html 在 Visual Studio 2008 中,使用 VSS 作为源码管理器,把文件签入后,文件会自动变为只读 ...

  10. http相关概念在iOS中的使用介绍

    http://www.cocoachina.com/ios/20160329/15773.html