which is the best way to set already defined int to null?
private int xy(){
int x = 5;
x = null; //-this is ERROR
return x;
}
so i choose this
private int xy(){
Integer x = 5;
x = null; //-this is OK
return (int)x;
}
Then i need something like :
if(xy() == null){
// do something
}
And my second question can i safely cast Integer to int?
Thanks for any response.
You can't. int
is a primitive value type - there's no such concept as a null
value for int
.
You can use null
with Integer
because that's a class instead of a primitive value.
It's not really clear what your method is trying to achieve, but you simply can't represent null
as an int
.
See more on this question at Stackoverflow