How to detect overflow when convert string to integer in java

前端 未结 3 1779
一生所求
一生所求 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:40

    Cast String value to Long and compare Long value with Integer.Max_value

        String bigStrVal="3147483647";        
        Long val=Long.parseLong(bigStrVal);
        if (val>Integer.MAX_VALUE){
            System.out.println("String value > Integer.Max_Value");
        }else
            System.out.println("String value < Integer.Max_Value");
    

提交回复
热议问题