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
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
}
}