ObjectUtils.isEmpty()和null区别
分配内存和赋值的区别
isEmpty() :判断值是否为空,即使已经分配内存,但没有赋值,依然是空 null :判断值是否为空,没有分配内存, 可能出现空指针异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class IsEmptyTest { public static void main(String[] args) { String s1 = new String(); String s2 = "abc" ; String s3 = "" ; System.out.println(s1 == null ); System.out.println(ObjectUtils.isEmpty(s1)); System.out.println( "---------------" ); System.out.println(s2 == null ); System.out.println(ObjectUtils.isEmpty(s2)); System.out.println( "---------------" ); System.out.println(s3 == null ); System.out.println(ObjectUtils.isEmpty(s3)); } false true --------------- false false --------------- false true |
Spring5.3之后StringUtils.isEmpty被弃用
今天在尝试自己做一个转换器时,被系统提示isEmpty被启用,但是学习视频中没有:
但是页面可以显示处自己转换器要实现的结果:
根据提示改为hasLength和hasText后,页面均没有实现想要的结果,显示为null
解决办法
就用isEmpyt,或者改为他描述的另一种方法:ObjectUtils.isEmpty
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/m_shang/article/details/122192212
查看更多关于关于ObjectUtils.isEmpty() 和 null 的区别的详细内容...