日期:2014-05-20  浏览次数:21180 次

判断int型是否为空
接收一个int id;使用之前判断它是否为空,怎么判断int型是否为空呢?
int null

------解决方案--------------------
转成Integer类型比较吧,int类型只能比较是不是不为0
------解决方案--------------------
int 型不会是空值。 变量定义的时候及时没赋值,也会给默认值。 如果要使用空值判断,用Integer
------解决方案--------------------
楼主是不要判断这个接受的id啊 要是这个id是页面接收来的 那倒有可能是空 人为地址栏输入 这样的话就要报错啊 可以写个方法:判断参数(id)是不是全是整数的数字:
  public class CommonChek {
/***
 * 判断参数是否全是数字(非数字,小数均无法通过,id不大可能是小数 所有这种情况要考虑)
 * @param str 字符串
 * @return  数字字符串返回t返rue.否则回false
 */
public static boolean isNumeric(String str){ 
Pattern pattern = Pattern.compile("[0-9]*"); 
Matcher matcher=  pattern.matcher(str);
boolean b=matcher.matches();
return b; 


/***
 * @function:字符串转为数字。
 * @param :字符串
 * @return :int
 * @describe:如果转换成功,返回一个整型的数字;如果转换失败,返回-1.
 */
public static int stringToInt(String parameter){
int i=-1;
if(parameter!=null && !"".equals(parameter)){
boolean b=CommonChek.isNumeric(parameter);
 if(b==true){
 i=Integer.parseInt(parameter);
 }
}
   return  i;
}
}
希望对你有帮助...
  
------解决方案--------------------
只有对象才能用null来判断,int是基本类型默认值为0.
可以转换为Integer再判断是否为null