在项目开发过程当中,面对各类各样的对象,若是稍不注意,就会发生NULL空指针报错;是否是很烦恼,特别是对重要的参数判读;java
通过总结,把各类类型的空判断进行了简单的封装,对新手仍是很方便的;数组
package com.xt.shop.until; import java.util.List; /** * <p>判断 对象 是否为空 * <p>返回值:为空 ? true : false * <p>建立人:geYang * <p>建立时间:2017.8.1 */ public class IsNull { /** * 判断字符串是否为空 * */ public static boolean isNull(String str){ if(str!=null){ str = str.trim(); } return str == null || str.isEmpty(); } /** * 判断List数组是否为空 * */ public static boolean isNull(List<?> list){ return list == null || list.isEmpty(); } /** * 判断Integer数组是否为空 * */ public static boolean isNull(Integer[] arr){ return arr==null || arr.length<1; } /** * 判断整数是否为空(ID) * */ public static boolean isNull(Integer num){ return num==null || num<1; } /** * 判断Double是否为空(金额) * */ public static boolean isNull(Double num){ return num==null || num<1; } /** *<p>方法说明: TODO 测试测试 **/ public static void main(String[] args) { String n = " "; System.out.println(isNull(n)); } }
就像这样,你们还能够根据本身的须要继续添加;测试