java判断一个对象是否为空
1. java判断一个对象是否为空是什么意思
更准确的说法是判断一个对象的引用是否指向空,null
引用指向的修改肯能会有空null的情况,一般是赋值语句修改引用指向。
判空:if(a==null){}
2. java里,判断一个对象是否是null,怎么判断
Object i = null;
try {
System.out.println(i.hashCode());
} catch (NullPointerException e) {
System.out.println("Object=null");
}
3. java对象为空的判断
/**
*判断对象或对象数组中每一个对象是否为空:对象为null,字符序列长度为0,集合类、Map为empty
*
*@paramobj
*@return
*/
(Objectobj){
if(obj==null)
returntrue;
if(objinstanceofCharSequence)
return((CharSequence)obj).length()==0;
if(objinstanceofCollection)
return((Collection)obj).isEmpty();
if(objinstanceofMap)
return((Map)obj).isEmpty();
if(objinstanceofObject[]){
Object[]object=(Object[])obj;
if(object.length==0){
returntrue;
}
booleanempty=true;
for(inti=0;i<object.length;i++){
if(!isNullOrEmpty(object[i])){
empty=false;
break;
}
}
returnempty;
}
returnfalse;
}
应用场景:
读取excel文件,转化为一个二维数组:Object[][]arrays
但是excel中有空行,所以需要过滤Object[][]arrays中的空的一维数组:
Java代码
/***
*过滤数组中的空元素
*
*
*@paramarrays
*@return
*/
publicstaticObject[][]filterEmpty(Object[][]arrays){
intsumNotNull=0;
/***
*统计非空元素的总个数
*/
for(inti=0;i<arrays.length;i++){
Objectobject=arrays[i];
if(!ValueWidget.isNullOrEmpty(object)
&&!SystemUtil.isNullOrEmpty((Object[])object)){//判断元素是否为空
sumNotNull=sumNotNull+1;
}
}
Object[][]filtedObjs=newObject[sumNotNull][];
intindex=0;
for(inti=0;i<arrays.length;i++){
Object[]object_tmp=arrays[i];
if(!ValueWidget.isNullOrEmpty(object_tmp)
&&!SystemUtil.isNullOrEmpty((Object[])object_tmp)){//判断元素是否为空
filtedObjs[index++]=object_tmp;
}
}
returnfiltedObjs;
}
判断对象的所有成员变量是否为空
Java代码
/***
*Determinewhethertheobject'sfieldsareempty
*
*@paramobj
*@paramisExcludeZero:true:数值类型的值为0,则当做为空;----false:数值类型的值为0,则不为空
*
*@return
*@throwsSecurityException
*@
*@throwsNoSuchFieldException
*@throwsIllegalAccessException
*/
(Objectobj,booleanisExcludeZero)
throwsSecurityException,IllegalArgumentException,
NoSuchFieldException,IllegalAccessException{
if(ValueWidget.isNullOrEmpty(obj)){//对象本身就为null
returntrue;
}
List<Field>fieldList=ReflectHWUtils.getAllFieldList(obj.getClass());
booleanisNull=true;
for(inti=0;i<fieldList.size();i++){
Fieldf=fieldList.get(i);
ObjectpropertyValue=null;
try{
propertyValue=getObjectValue(obj,f);
}catch(NoSuchFieldExceptione){
e.printStackTrace();
}
if(!ValueWidget.isNullOrEmpty(propertyValue)){//字段不为空
if(){//是数字
if(!((Integer)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}elseif(propertyValueinstanceofDouble){//是数字
if(!((Double)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}elseif(propertyValueinstanceofFloat){//是数字
if(!((Float)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}elseif(propertyValueinstanceofShort){//是数字
if(!((Short)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}else{
isNull=false;
break;
}
}
}
returnisNull;
}
测试:
Java代码
@Test
publicvoidtest_isNullObject()throwsSecurityException,
IllegalArgumentException,NoSuchFieldException,
IllegalAccessException{
Person2p=newPerson2();
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代码
importjava.sql.Timestamp;
publicclassPerson2{
privateintid;
privateintage;
privatedoubleweight;
privateStringpersonName;
privateTimestampbirthdate;
publicStringidentitify;
protectedStringaddress;
Stringphone;
}
4. Java中判断对象为空的问题
if(pb==null)
{
System.out.println(“为空”);
}
equals比较两个对象的内容是否相同
== 比较两个对象是否是同一对象。
5. java 怎样判断一个对象是否为空
Item item = new Item();这个对象肯定是为空的
错了,这个对象已经分配了内存,不是空的,用System.out.println(item)打印就知道已版经存在地址,如果是权空,打印null;
判断一个对象是否为空,就是按那个条件判断,没有错,System.out.println();是控制台比较实用的调试,测试方法
6. java怎么判断一个对象是否为空
new一个空的对象占用的内存是8字节
里面有东西就会超过 8byte
7. java判断对象是否为空
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;
}
8. 如何判断一个对象是否为空 javascript
/**
*判断传入参数是否为空
*/
functionisEmpty(v){
switch(typeofv){
case'undefined':
returntrue;
case'string':
if(v.replace(/(^[ ]*)|([ ]*$)/g,'').length==0)
returntrue;
break;
case'boolean':
if(!v)
returntrue;
break;
case'number':
if(0===v||isNaN(v))
returntrue;
break;
case'object':
if(null===v||v.length===0)
returntrue;
for(variinv){
returnfalse;
}
returntrue;
}
returnfalse;
}
9. java怎么判断一个类为空
Guest guest = (Guest)session.getAttribute("guest");
if(guest==null){
getServletContext().getRequestDispatcher(
"/error.jsp").forward(request, response);
在java中null并不是一个对象,但可以判断一个引用类型数据是否为空,用==来判断,而且null同时用来释放内存,所以可以给对象用来赋值或者判断。