Java 数据类型非空与非零判断工具类

public class VerificationUtil {
    //判断 Integer类型数据 非零
    public static boolean notZero(Integer num) {
        if (num == null) {
            return false;
        } else return num != 0;
    }
    //判断 Integer 类型数据 零
    public static boolean zero(Integer num) {
        if (num == null) {
            return true;
        } else return num == 0;
    }
    //判断 Integer类型数据 非零
    public static boolean notZero(Long num) {
        if (num == null) {
            return false;
        } else return num != 0;
    }
    //判断 Long类型数据 零
    public static boolean zero(Long num) {
        if (num == null) {
            return true;
        } else return num == 0;
    }
    //判断 Double类型数据 非零
    public static boolean notZero(Double num) {
        if (num == null) {
            return false;
        } else return num != 0;
    }
    //判断 Double类型数据 零
    public static boolean zero(Double num) {
        if (num == null) {
            return true;
        } else return num == 0;
    }
    //判断 Integer 类型数据 非空
    public static boolean isNotNull(Integer num) {
        return num != null;
    }
    //判断 Integer 类型数据 非空
    public static boolean isNull(Integer num) {
        return num == null;
    }
    //判断 Long 类型数据 非空
    public static boolean isNotNull(Long num) {
        return num != null;
    }
    //判断 Long 类型数据 空
    public static boolean isNull(Long num) {
        return num == null;
    }
    //判断 Double 类型数据 非空
    public static boolean isNotNull(Double num) {
        return num != null;
    }
    //判断 Double 类型数据 空
    public static boolean isNull(Double num) {
        return num == null;
    }
    //判断 Date 类型数据 非空
    public static boolean isNotNull(Date d) {
        return d != null;
    }
    //判断 Date 类型数据 空
    public static boolean isNull(Date d) {
        return d == null;
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }
在后端做非空判断是非常重要的,因为它可以有效防止指针异常(NullPointerException)的发生。以下是在后端做非空判断时需要注意的几点: 1. 对于基本数据类型(如 int、double、boolean 等),不需要进行非空判断,因为它们的默认值都不为 null; 2. 对于对象类型,需要对其进行非空判断,否则可能会出现指针异常; 3. 在判断对象是否为时,应该使用对象的 equals() 方法来判断,而不是使用 == 运算符; 4. 对于集合类型(如 List、Set、Map 等),需要判断其是否为,以及集合中的元素是否为; 5. 可以使用工具类或框架提供的方法来简化非空判断的代码,如 Apache Commons Lang 的 StringUtils 类或 Spring Framework 的 Assert 类。 以下是一些示例代码,演示了如何在后端做非空判断: ```java // 判断字符串是否为 public boolean isNullOrEmpty(String str) { return str == null || str.isEmpty(); } // 判断对象是否为 public boolean isNull(Object obj) { return obj == null; } // 判断集合是否为 public boolean isNullOrEmpty(Collection<?> collection) { return collection == null || collection.isEmpty(); } // 判断集合中的元素是否为 public boolean hasNullElement(Collection<?> collection) { for (Object obj : collection) { if (obj == null) { return true; } } return false; } ``` 在实际开发中,需要根据具体的业务场景选择合适的非空判断方式,并保证代码的可读性和健壮性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java之眼

创作不易,一起努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值