Cast a Null String into Integer

前端 未结 7 878
闹比i
闹比i 2021-01-01 18:40

Is there any way to cast a null to Integer. The null is actually a String, which i am passing in my service layer that accepts it as an Integer. So, whenever i try to cast a

7条回答
  •  隐瞒了意图╮
    2021-01-01 18:53

    If you are sure you only have to handle nulls,

    int i=0;
    i=(str==null?i:Integer.parseInt(str));
    System.out.println(i);
    

    for non integer strings it will throw Numberformat exception

提交回复
热议问题