「java对象为空」java 空对象

博主:adminadmin 2022-11-23 16:50:08 59

本篇文章给大家谈谈java对象为空,以及java 空对象对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 怎样判断一个对象是否为空?

Item item = new Item();这个对象肯定是为空的

错了,这个对象已经分配了内存,不是空的,用System.out.println(item)打印就知道已经存在地址,如果是空,打印null;

判断一个对象是否为空,就是按那个条件判断,没有错,System.out.println();是控制台比较实用的调试,测试方法

java对象为空的判断

/** 

     * 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty 

     *  

     * @param obj 

     * @return 

     */  

    public static boolean isNullOrEmpty(Object obj) {  

        if (obj == null)  

            return true;  

  

        if (obj instanceof CharSequence)  

            return ((CharSequence) obj).length() == 0;  

  

        if (obj instanceof Collection)  

            return ((Collection) obj).isEmpty();  

  

        if (obj instanceof Map)  

            return ((Map) obj).isEmpty();  

  

        if (obj instanceof Object[]) {  

            Object[] object = (Object[]) obj;  

            if (object.length == 0) {  

                return true;  

            }  

            boolean empty = true;  

            for (int i = 0; i  object.length; i++) {  

                if (!isNullOrEmpty(object[i])) {  

                    empty = false;  

                    break;  

                }  

            }  

            return empty;  

        }  

        return false;  

    }  

应用场景:

读取excel文件,转化为一个二维数组:Object[][] arrays

但是excel中有空行,所以需要过滤Object[][] arrays中的空的一维数组:

Java代码  

/*** 

     * 过滤数组中的空元素 

     *  

     *  

     * @param arrays 

     * @return 

     */  

    public static Object[][] filterEmpty(Object[][] arrays) {  

        int sumNotNull = 0;  

        /*** 

         * 统计非空元素的总个数 

         */  

        for (int i = 0; i  arrays.length; i++) {  

            Object object = arrays[i];  

            if (!ValueWidget.isNullOrEmpty(object)  

                     !SystemUtil.isNullOrEmpty((Object[]) object)) {// 判断元素是否为空  

                sumNotNull = sumNotNull + 1;  

            }  

        }  

        Object[][] filtedObjs = new Object[sumNotNull][];  

        int index = 0;  

        for (int i = 0; i  arrays.length; i++) {  

            Object[] object_tmp = arrays[i];  

            if (!ValueWidget.isNullOrEmpty(object_tmp)  

                     !SystemUtil.isNullOrEmpty((Object[]) object_tmp)) {// 判断元素是否为空  

                filtedObjs[index++] = object_tmp;  

            }  

        }  

        return filtedObjs;  

    }  

判断对象的所有成员变量是否为空

Java代码  

/*** 

     * Determine whether the object's fields are empty 

     *  

     * @param obj 

     * @param isExcludeZero  :true:数值类型的值为0,则当做为空;----false:数值类型的值为0,则不为空 

     *  

     * @return 

     * @throws SecurityException 

     * @throws IllegalArgumentException 

     * @throws NoSuchFieldException 

     * @throws IllegalAccessException 

     */  

    public static boolean isNullObject(Object obj, boolean isExcludeZero)  

            throws SecurityException, IllegalArgumentException,  

            NoSuchFieldException, IllegalAccessException {  

        if(ValueWidget.isNullOrEmpty(obj)){//对象本身就为null  

            return true;  

        }  

        ListField fieldList = ReflectHWUtils.getAllFieldList(obj.getClass());  

        boolean isNull = true;  

        for (int i = 0; i  fieldList.size(); i++) {  

            Field f = fieldList.get(i);  

            Object propertyValue = null;  

            try {  

                propertyValue = getObjectValue(obj, f);  

            } catch (NoSuchFieldException e) {  

                e.printStackTrace();  

            }  

  

            if (!ValueWidget.isNullOrEmpty(propertyValue)) {// 字段不为空  

                if (propertyValue instanceof Integer) {// 是数字  

                    if (!((Integer) propertyValue == 0  isExcludeZero)) {  

                        isNull = false;  

                        break;  

                    }  

                } else if (propertyValue instanceof Double) {// 是数字  

                    if (!((Double) propertyValue == 0  isExcludeZero)) {  

                        isNull = false;  

                        break;  

                    }  

                }else if (propertyValue instanceof Float) {// 是数字  

                    if (!((Float) propertyValue == 0  isExcludeZero)) {  

                        isNull = false;  

                        break;  

                    }  

                }else if (propertyValue instanceof Short) {// 是数字  

                    if (!((Short) propertyValue == 0  isExcludeZero)) {  

                        isNull = false;  

                        break;  

                    }  

                }else {  

                    isNull = false;  

                    break;  

                }  

            }  

        }  

        return isNull;  

    }  

 测试:

Java代码  

@Test  

    public void test_isNullObject() throws SecurityException,  

            IllegalArgumentException, NoSuchFieldException,  

            IllegalAccessException {  

        Person2 p = new Person2();  

        Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));  

        Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));  

  

        p.setAddress("beijing");  

        Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, true));  

        Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));  

  

        p.setAddress(null);  

        p.setId(0);  

        Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));  

        Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));  

  

    }  

 Person2 源代码(省略getter,setter方法):

Java代码  

import java.sql.Timestamp;  

  

public class Person2 {  

    private int id;  

    private int age;  

    private double weight;  

    private String personName;  

    private Timestamp birthdate;  

    public String identitify;  

    protected String address;  

    String phone;  

      

}

java 中空对象是什么概念

所谓空对象就是指向NULL的,即未分配堆内存的,也就是没有new的。

例如

class A{。。。}

A a;

a==null?true:false//为true。

A a=new A();

a==null?true:false//为false。

java怎么判断对象为null

public static void main(String[] args) {

//person是对象newPerson的引用

Person person = new Person();

//判断是否为空

if(person==null){

//如果为空 打印对象为空

System.out.println("对象为空");

}else{

//如果为不为空 打印person

System.out.println("对象为"+person);

}

}

Java中判断对象为空的问题

首先来看一下工具StringUtils的判断方法: 

一种是org.apache.commons.lang3包下的; 

另一种是org.springframework.util包下的。这两种StringUtils工具类判断对象是否为空是有差距的:

StringUtils.isEmpty(CharSequence cs); //org.apache.commons.lang3包下的StringUtils类,判断是否为空的方法参数是字符序列类,也就是String类型StringUtils.isEmpty(Object str); //而org.springframework.util包下的参数是Object类,也就是不仅仅能判断String类型,还能判断其他类型,比如Long等类型。12345

从上面的例子可以看出第二种的StringUtils类更实用。

下面来看一下org.apache.commons.lang3的StringUtils.isEmpty(CharSequence cs)源码:public static boolean isEmpty(final CharSequence cs) {        return cs == null || cs.length() == 0;

接下来是org.springframework.util的StringUtils.isEmpty(Object str)源码:public static boolean isEmpty(Object str) {        return (str == null || "".equals(str));

基本上判断对象是否为空,StringUtils.isEmpty(Object str)这个方法都能搞定。

接下来就是判断数组是否为空

list.isEmpty(); //返回boolean类型。

java对象为空的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 空对象、java对象为空的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。