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同時用來釋放內存,所以可以給對象用來賦值或者判斷。