導航:首頁 > 編程語言 > java空對象

java空對象

發布時間:2022-09-28 07:18:24

java中怎麼判斷一個對象是不是為空

這樣寫是存在問題的,如果pb為空,pb.equals(null))會出現空指針異常.
if(null == pb)
System.out.println("為空");
else
System.out.println("不為空");

Ⅱ java中一個void修飾的方法無返回值,那麼有是不是返回一個空對象,有空對象這種說法嗎

空對象是指null。void表示沒有返回,包括沒有返回空對象。
你可以測試下void方法內部,return null;是會報編譯錯誤的。只能return;

Ⅲ java程序中使用空對象會出現什麼錯誤

java程序中使用空對象
如果是對空對象賦值 不會報錯
而對其取值 就會 拋出一個異常 NullPoniterException 空指針異常
你可以用 try ....catch 語句捕獲這個異常 就不會報錯了

Ⅳ 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;

}

Ⅳ Java中判斷對象為空的問題

Ⅵ java 怎樣判斷一個對象是否為空

new之後就在堆內存中開辟了一定的空間來存儲對象,同時棧內存中的引用(就是item中存放的地址)指向對應的堆內存地址;null表示內存中沒有這個對象。

閱讀全文

與java空對象相關的資料

熱點內容
十大現實變成游戲的小說 瀏覽:35
香港愛情尺寸度電影推薦 瀏覽:575
java用方法實現加減 瀏覽:615
預警編程在哪裡學 瀏覽:741
黑暗與曙光配置文件 瀏覽:949
數據通信硬體是什麼 瀏覽:245
s3c2440外部中斷代碼 瀏覽:491
許君聰二龍湖浩哥的電影 瀏覽:510
騎士助手文件夾的名字 瀏覽:825
風雲雄霸天下小說全集txt下載 瀏覽:532
審計大數據情況 瀏覽:862
隨著網路技術迅猛發展對 瀏覽:737
韓國十大神級電影網站 瀏覽:878
平板電腦虛擬按鍵配置文件 瀏覽:374
歐美動作愛情 瀏覽:915
word2013更改圖片 瀏覽:980
win10plsql 瀏覽:819
香港電影開頭一個女的在床上自慰 瀏覽:512
win10cdromsys下載 瀏覽:30
桌面程序hibernate 瀏覽:14

友情鏈接