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

解决方案 »

  1.   

    转成Integer类型比较吧,int类型只能比较是不是不为0
      

  2.   

    int 型不会是空值。 变量定义的时候及时没赋值,也会给默认值。 如果要使用空值判断,用Integer
      

  3.   

    int类型默认为0,String默认才是null
      

  4.   

    包装一下吧,int类型怎么可能为null
      

  5.   

    楼主是不要判断这个接受的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;
    }
    }
    希望对你有帮助...
      
      

  6.   

    要么包装成Integer类型,要么判断是否为number
      

  7.   

    要么包装成Integer,判断(Integer)id ==null;             要么直接判断id == 0;
      

  8.   

    只有对象才能用null来判断,int是基本类型默认值为0.
    可以转换为Integer再判断是否为null