如何设置为int null值? Android版的Java [英] How to set to int value null? Java Android
本文介绍了如何设置为int null值? Android版的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是设置已经定义的内部以最好的方式的无效
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
}
和我的第二个问题,我可以放心地投整数为int?
And my second question can i safely cast Integer to int?
感谢您的任何答复。
推荐答案
在这种情况下,我会避免使用空
在一起。
In this case, I would avoid using null
all together.
只要使用 1
作为你的空
如果您需要 1
是一个可以接受(NOT NULL)值,然后用浮动
来代替。由于所有的真正的答案将是整数,让你空 0.1
If you need -1
to be an acceptable (not null) value, then use a float
instead. Since all your real answers are going to be integers, make your null 0.1
或者,找一个值,该 X
绝不会像 Integer.MAX_VALUE的
或东西。
Or, find a value that the x
will never be, like Integer.MAX_VALUE
or something.
这篇关于如何设置为int null值? Android版的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文