Java: arr==null vs arr.length==0
当 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的更多相关文章
- 数组中array==null和array.length==0的区别
//代码public class Test1 { public static void main(String[] args) { int[] a1 = new int[0]; int[] a2 = ...
- java——arr == null || arr.length == 0
这两者是不同的: arr == null; int[] arr = null; arr.length == 0; int[] arr =new int[0];
- protobuf接口调用报错:java.nio.charset.MalformedInputException: Input length = 1
使用protobuf定义的接口api发起http请求报错,日志如下: [-- ::] DEBUG AbstractPool: - server updated, node=, server={ nod ...
- 【Java】 剑指offer(53-2) 0到n-1中缺失的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个 ...
- Java字符串null相加
Java字符串null相加 最近和同事讨论了下面的一段代码: String a = null; a += a; System.out.println(a); 运行结果: nullnull 本着学习的态 ...
- 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 ...
- Java 获取客户端ip返回127.0.0.1问题
Java开发中使用 request.getRemoteAddr 获取客户端 ip ,返回结果始终为127.0.0.1.原因是服务器使用了nginx反向代理. 解决办法:在nginx配置文件nginx. ...
- java中NULL与" "的区别
null是没有地址""是有地址但是里面的内容是空的 null和""的区别 问题一: null和""的区别 String s=null; st ...
- java中null和""的区别
问题一: null和""的区别 String s=null; s.trim()就会抛出为空的exception String s=""; s.trim()就不会 ...
随机推荐
- Webscan360的防御与绕过
这两天给360做了一个webscan360的总结,结果补丁还没有出来,就被人公布到了91org上面,既然公开了,那我就做一个总结 首先我们贴上它最新的防御正则 \<.+javascript:wi ...
- Css - 渲染按钮
基本的css3按钮渲染 <style type="text/css"> background: #f7f7f7; /* for Webkit */ background ...
- 《Lucene实战(第2版)》 配书代码在IDEA下的编译方法
参考: hankcs http://www.hankcs.com/program/java/lucene-combat-2nd-edition-book-with-code-compiled-unde ...
- [转]ASP.NET会话(Session)保存模式
本文转自:http://blog.csdn.net/cityhunter172/article/details/727743 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦 ...
- IOS第四天(6:答题区按钮点击和乱序)
#pragma mark - 答题区按钮点击方法 - (void)answerClick:(UIButton *)button { // 1. 如果按钮没有字,直接返回 ) return; // 2. ...
- Export-XLSX PowerShell generate real Excel XLSX files without Excel and COM
http://gallery.technet.microsoft.com/scriptcenter/Export-XLSX-PowerShell-f2f0c035
- Nvidia VertexTextureFetch Water
http://http.download.nvidia.com/developer/SDK/Individual_Samples/samples.html http://http.download.n ...
- Memcached 笔记与总结(3)安装 php-memcache(windows 系统下)
在 windows 下安装 php-memcache,需要下载编译好的 memcached.dll. 要找到可用的 dll 文件,需要根据 php.ini 中的 3 个参数来选择 dll 文件: ① ...
- STM32全球唯一ID读取方法
产品唯一的身份标识非常适合:● 用来作为序列号(例如USB字符序列号或者其他的终端应用)● 用来作为密码,在编写闪存时,将此唯一标识与软件加解密算法结合使用,提高代码在闪存存储器内的安全性.● 用来激 ...
- [转]为何TCP/IP协议栈设计成沙漏型的
http://m.blog.csdn.net/blog/dog250/18959371 前几天有人回复我的一篇文章问,为何TCP/IP协议栈设计成沙漏型的.这个问题问得好!我先不谈为何它如此设计,我一 ...