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 ...
随机推荐
- [转帖]linux tree命令--显示目录的树形结构
linux tree命令--显示目录的树形结构 版权声明:iamqilei@qq.com https://blog.csdn.net/u011729865/article/details/533 ...
- socket通信原理三次握手和四次握手详解
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- .Net中EF通用数据层小结
增删改查: using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; u ...
- C# Note11:如何优雅地退出WPF应用程序
前言 I should know how I am supposed to exit my application when the user clicks on the Exit menu item ...
- CBV源码分析+APIVIew源码分析
{drf,resful,apiview,序列化组件,视图组件,认证组件,权限组件,频率组件,解析器,分页器,响应器,URL控制器,版本控制} 一.CBV源码分析准备工作: 新建一个Django项目 写 ...
- Git发生SSL certificate problem: certificate ha错误的解决方法
这两天,不知道为什么,用Git提交代码到服务器时,总出现SSL certificate problem: unable to get local issuer certificate while ac ...
- java 调用 wsdl形式的webservice 示例
import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceExc ...
- python之路--装饰器
二 .通用装饰器的写法 python里面的动态代理. 存在的意义: 在不破坏原有的函数和原有函数的调用基础上,给函数添加新的功能 def wrapper(fn): # fn是目标函数. def inn ...
- 用mescroll实现无限上拉增加数据,下拉刷新数据 (学习笔记)
最近自己做一个web app需要用到上拉查询下页数据,网上看了很多很多帖子,发现并不能快速的套用,总是会出现各种问题无法使用,于是无奈自己跑去看了官方api文档,终于做了出来,至此做个笔记,以后用到可 ...
- python学习笔记(10)--组合数据类型(集合类型)
集合类型 集合是多个元素的无序组合,每个元素唯一,不存在相同类型,每个元素是不可变类型.用{}表示,元素间用逗号分隔.建立结合类型用{},或set函数,如果是空集合必须用set. >>&g ...