Cast a Null String into Integer

前端 未结 7 936
闹比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:51

    You cannot cast from String to Integer.
    Java data types are of two kinds: primitive and reference. Primitive types are: byte, short, int, long, char, float, double. The reference types are: class, interface, array.
    byte -> short -> int -> long -> float -> double. is allow, Or The casting may be of its own type or to one of its subclass or superclasss types or interfaces.
    So this is a ex. of function take a look

        public int getInteger(String no) {
        if (no != null) {
            return Integer.parseInt(no); //convert your string into integer 
        } else {
            return 0; // or what you want to return if string is Null
        }
    }
    

提交回复
热议问题