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()就不会 ...
随机推荐
- (转载)c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值
第一种方法: 用委托,Form2和Form3是同一组 Form2 C#代码 using System; using System.Collections.Generic; using System.C ...
- winform学习之----图片控件应用(上一张,下一张)
示例1: int i = 0; string[] path = Directory.GetFiles(@"C:\Users\Administrator\Desktop\图片&q ...
- Resume简历中装B的词汇总结大全
1. Accelerated 35. Empowered 69. Motivated 2. Accomplished 36. Enabled 70. Negotiated 3. Achieved 37 ...
- 通过SEP禁用USB
1 Introduction 1.1 Scope This document provides comprehensive information of the reinforce ...
- MSBuild学习记录
参考资料 官方文档 MSBuild入门 MSBuild的简单介绍与使用
- 19. 求平方根序列前N项和
求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...
- Ubuntu 安装 ImageMagic(6.9.1-6)及 PHP 的 imagick (3.0.1)扩展
关于 ImageMagic 和 imagick 的介绍,见<图片处理神器ImageMagick以及PHP的imagick扩展> 和 <Ubuntu下安装ImageMagick和Mag ...
- PHP 开启短标签
<?=STATIC_URL?> 让上面的语句可以正常运行,等同于下面的语句 <?php echo STATIC_URL;?> 可以在 php.ini 中找到 short_ope ...
- 一些Discuz!代码
首行缩进2字符 [code][p=20, 2, left]首行缩进2字符[/p][/code]
- linux下创建和删除软、硬链接
linux下创建和删除软.硬链接 在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号.文件属性保存在索引结点里,在访问文件时,索引结点被复制 ...