簡單封裝JAVA空判斷

    在項目開發過程當中,面對各類各樣的對象,若是稍不注意,就會發生NULL空指針報錯;是否是很煩惱,特別是對重要的參數判讀;java

    通過總結,把各類類型的空判斷進行了簡單的封裝,對新手仍是很方便的;數組

package com.xt.shop.until;

import java.util.List;

/**
 * <p>判斷 對象 是否爲空
 * <p>返回值:爲空  ? true : false
 * <p>建立人:geYang
 * <p>建立時間:2017.8.1
 */
public class IsNull {

	/**
	 * 判斷字符串是否爲空
	 * */
	public static boolean isNull(String str){
		if(str!=null){
			str = str.trim();
		}
		return str == null || str.isEmpty();
	}
	
	/**
	 * 判斷List數組是否爲空
	 * */
	public static boolean isNull(List<?> list){
		return list == null || list.isEmpty(); 
	}
	
	/**
     * 判斷Integer數組是否爲空
     * */
	public static boolean isNull(Integer[] arr){
	    return arr==null || arr.length<1;
	}
	
	/**
	 * 判斷整數是否爲空(ID)
	 * */
	public static boolean isNull(Integer num){
	    return num==null || num<1;
	}
	
	/**
	 * 判斷Double是否爲空(金額)
	 * */
	public static boolean isNull(Double num){
	    return num==null || num<1;
	}
	
	/**
	 *<p>方法說明: TODO 測試測試
	 **/
	public static void main(String[] args) {
		String n = " ";
		System.out.println(isNull(n));
	}
}

就像這樣,你們還能夠根據本身的須要繼續添加;測試

相關文章
相關標籤/搜索