How to detect overflow when convert string to integer in java

前端 未结 3 1757
一生所求
一生所求 2021-01-12 04:02

if I want to convert a string into an int in java do you know if there is a way for me to detect overflow? by that I mean the string literal actually represents a value whic

3条回答
  •  一生所求
    2021-01-12 04:23

    You don't need to do anything.

    As the Javadoc for Integer.parseInt(str, radix) states, a NumberFormatException is thrown if the value represented by the String is not representable as an int value; i.e. if its magnitude is too big. This is described as a separate case from the case where str is badly formatted, so it is clear (to me) that this is what is meant. (And you can confirm this by reading the source code.)

提交回复
热议问题