java 各种数据类型判断为空
一,基本数据类型
八种基本类型有默认值
http://www.runoob.com/java/java-basic-datatypes.html
二,String 对象
// 判断String为空
//String s = "";
//String s = null;
String s = " ";
s = s.trim(); // 处理" "
// 方法1
if (s == null || "".equals(s)) { // null,""," "
System.out.println("String 为空1");
}
// 方法2
if (s == null || s.length() <= 0) { // 推荐 // null,""," "
System.out.println("String 为空2");
} else if (s.length() == 1 && Character.isWhitespace(s.charAt(0)) == true) {
System.out.println("String 为空5");
}
// 方法3
if (s == null || s.isEmpty()) { // null,""," "
System.out.println("String 为空3");
}
// 方法4
if (s == null || s == "") { // 不推荐 // null,""
System.out.println("String 为空4");
}
apache工具类
StringUtils.isBlank(s)
三,数组
// 判断数组为空
//String[] arr = null;
String[] arr = new String[0];
//String[] arr = new String[2]; // 有长度,不为空
if (arr == null || arr.length == 0) {
System.out.println("数组 为空1");
}
apache工具类
ArrayUtils.isEmpty(arr)
四,集合(List,Map,Set)
// 判断list为空(Map、Set同list)
//List<String> strList = null;
List<String> strList = new ArrayList<String>();
if (strList == null || strList.size() == 0) {
System.out.println("list 为空1");
}
if (strList == null || strList.isEmpty()) { // 推荐
System.out.println("list 为空2");
}
spring工具类
CollectionUtils.isEmpty(strList);
java 各种数据类型判断为空的更多相关文章
- Java中判断非空对象.
Java中经常会遇到判断非空的时候. 有的时候判断了非空但是还是报空指针,为什么.? 判断的时候一般都会判断两次.类似于: Org o = new Org(); if ( o.getId()!=nul ...
- java中的不为空判断
String不为空判断 if(null != str && !"".equals(str)) List不为空判断 if(list!=null && ...
- java的数据类型:基本数据类型和引用数据类型
Java数据类型的基本概念 数据类型在计算机语言里面,是对内存位置的一个抽象表达方式,可以理解为针对内存的一种抽象的表达方式. 开始接触每种语言的时候,都会存在对数据类型的认识,有复杂的,有复杂的,各 ...
- Java 基本数据类型 及 == 与 equals 方法的区别
Java数据类型分为基本数据类型与引用数据类型. 1 基本数据类型 byte:Java中最小的数据类型,在内存中占1个字节(8 bit),取值范围-128~127,默认值0 short:短整型,2个字 ...
- Java基本数据类型及其封装器的一些千丝万缕的纠葛
一些概念 想必大家都知道Java的基础数据类型有:char.byte.short.int.long.float.double.boolean 这几种,与C/C++等语言不同的是,Java的基础 ...
- JAVA 变量 数据类型 运算符 知识小结
---------------------------------------------------> JAVA 变量 数据类型 运算符 知识小结 <------------------ ...
- 第十九节:Java基本数据类型,循环结构与分支循环
基本数据类型 Java中的基本数据类型,有8种,在Java中有四种类型,8种基本数据类型. 字节 boolean 布尔型为1/8 byte 字节类型为1 short 短整型为2 char 字符型为2 ...
- golang interface判断为空nil
要判断interface 空的问题,首先看下其底层实现. interface 底层结构 根据 interface 是否包含有 method,底层实现上用两种 struct 来表示:iface 和 ef ...
- Java 基本数据类型 && 位运算
1. Java基本数据类型 1.1 数据类型示意图 类型 字节数 范围 byte 1 -128~127 short 2 -32768~32767 int 4 -231~231-1 long 8 -26 ...
随机推荐
- [转帖]SSH 的 三种代理功能.
https://www.zhihu.com/people/skywind3000/activities 之前用过frp 知道 ssh 有一 反向代理的功能 没想到 还有这么多高级功能 收藏一下. ss ...
- TextView不用ScrollViewe也可以滚动的方法
转自:http://www.jb51.net/article/43377.htm android TextView不用ScrollViewe也可以滚动的方法. TextView textview = ...
- EXAMPLE FOR PEEWEE 多姿势使用 PEEWEE
使用 PEEWEE 断断续续的差不多已经三个年头了,但是没有像这次使用这么多的特性和功能,所以这次一并记录一下,需要注意的地方和一些使用细节,之后使用起来可能会更方便. 因为是使用的 SQLAched ...
- thinkphp5 实现搜索分页能下一页保留搜索条件
正常情况下: 搜索后分页了,点击第二页,进入页面之前的搜索条件没有了. 如代码 $keywords=$this->request->param('keywords'); $this-> ...
- linux环境 :LIBRARY_PATH, LD_LIBRARY_PATH区别
参考: https://www.cnblogs.com/mylinux/p/4955448.html LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量,二者的含义和 ...
- centOS7 下配置和启动maria数据库
从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install mariadb mariadb-server systemct ...
- c++ 怎么输出保留2位小数的浮点数
//添加头文件 #include<iomanip> //定义变量 folat a=9.1; cout<<setiosflags(ios::fixed)<<setpr ...
- ceph 重启,停止,开始
systemctl restart ceph-mon@mon-node3 systemctl stop ceph-mon@mon-node3 systemctl start ceph-mon@mon- ...
- BZOJ5418[Noi2018]屠龙勇士——exgcd+扩展CRT+set
题目链接: [Noi2018]屠龙勇士 题目大意:有$n$条龙和初始$m$个武器,每个武器有一个攻击力$t_{i}$,每条龙有一个初始血量$a_{i}$和一个回复值$p_{i}$(即只要血量为负数就一 ...
- 微信小程序——报错汇总
tabBar.list[2].selectedIconPath 文件不存在 很明显是文件名错了,定义的my-acive,少写了个t,眼睛出问题了~ module "static/vant/c ...