導航:首頁 > 編程語言 > java如何判斷對象為空

java如何判斷對象為空

發布時間:2022-08-17 17:26:34

1. java中怎樣判斷一個對象是否為空

if(obj==null)

希望採納。戰狼班Java培訓學校

2. 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;
}

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怎麼判斷int是否為空

intpoint;Stringval=point+"";而是一個中間變數,intpoint=GiftInfo。getPoints();在資料庫表中有屬性欄位名稱points,類型為int。

java中的類型有基本類型:intlongdouble等,判斷的為0(int)或者0。0(doublefloat)對象類型:包括基本類型的包裝類,Integer(int)DoubleFloat不給賦值的時候就為null了還有一個特殊的String,本身是對象類型也是基本類型。

面向對象

Java是一個面向對象的語言。對程序員來說,這意味著要注意應中的數據和操縱數據的方法(method),而不是嚴格地用過程來思考。在一個面向對象的系統中,類(class)是數據和操作數據的方法的集合。數據和方法一起描述對象(object)的狀態和行為。

每一對象是其狀態和行為的封裝。類是按一定體系和層次安排的,使得子類可以從超類繼承行為。在這個類層次體系中有一個根類,它是具有一般行為的類。Java程序是用類來組織的。

以上內容參考:網路-Java

5. java怎麼判斷一個對象是否為空

if(對象名==null){
System.out.print("對象為空");
}

6. java 如何實現判斷一個對象所有的屬性是否為空

其實不用那麼麻煩,只用定義一個方法,然後使用下面的代碼片段來判斷欄位是否為空:

for (Field f : obj.getClass().getDeclaredFields()) {
f.setAccessible(true);
if (f.get(obj) == null) { //判斷欄位是否為空,並且對象屬性中的基本都會轉為對象類型來判斷
......
}
}

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

Item item = new Item();這個對象肯定是為空的
錯了,這個對象已經分配了內存,不是空的,用System.out.println(item)列印就知道已經存在地址,如果是空,列印null;

判斷一個對象是否為空,就是按那個條件判斷,沒有錯,System.out.println();是控制台比較實用的調試,測試方法

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

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

閱讀全文

與java如何判斷對象為空相關的資料

熱點內容
pythonclass使用方法 瀏覽:221
移動加密軟體去哪下載 瀏覽:281
php彈出alert 瀏覽:207
吉林文檔課件加密費用 瀏覽:131
感測器pdf下載 瀏覽:284
隨車拍app綁定什麼設備 瀏覽:896
方維團購系統源碼 瀏覽:991
linux反彈shell 瀏覽:158
列印機介面加密狗還能用嗎 瀏覽:299
二板股票源碼 瀏覽:448
度人經pdf 瀏覽:902
怎麼配置android遠程伺服器地址 瀏覽:960
java程序員看哪些書 瀏覽:943
什麼app可以免費和外國人聊天 瀏覽:797
pdf手寫筆 瀏覽:182
別永遠傷在童年pdf 瀏覽:990
愛上北斗星男友在哪個app上看 瀏覽:421
主力散戶派發源碼 瀏覽:671
linux如何修復伺服器時間 瀏覽:61
榮縣優途網約車app叫什麼 瀏覽:479