Java 如何检查 int 是否不为 null 或为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3820602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether a int is not null or empty?
提问by
I really dont know if this is possible or not.
But I am strucking in a place where I want to check an int value is null
or not, to call different methods.
我真的不知道这是否可能。但是我在一个我想检查 int 值是否存在的地方感到震惊null
,以调用不同的方法。
Is there any way to do it?
有什么办法吗?
Actually, variable comes from a browser with a null
value.
I cannot change the int
declaration to Integer
. Because it has it own consequences.
实际上,变量来自带有null
值的浏览器。我无法将int
声明更改为Integer
. 因为它有它自己的后果。
Any suggestions?
有什么建议?
采纳答案by Bozho
int
variables can't be null
int
变量不能 null
If a null
is to be converted to int
, then it is the converter which decides whether to set 0
, throw exception, or set another value (like Integer.MIN_VALUE
). Try to plug your own converter.
如果将 anull
转换为int
,则由转换器决定是设置0
、抛出异常还是设置另一个值(如Integer.MIN_VALUE
)。尝试插入您自己的转换器。
回答by Andriy Sholokh
Possibly browser returns String representation of some integer value? Actually int can't be null. May be you could check for null, if value is not null, then transform String representation to int.
浏览器可能返回某个整数值的字符串表示?实际上 int 不能为空。可能您可以检查 null,如果 value 不为 null,则将 String 表示形式转换为 int。
回答by Siri
if your int variable is declared as a class level variable (instance variable) it would be defaulted to 0. But that does not indicate if the value sent from the client was 0 or a null. may be you could have a setter method which could be called to initialize/set the value sent by the client. then you can define your indicator value , may be a some negative value to indicate the null..
如果您的 int 变量被声明为类级别变量(实例变量),它将默认为 0。但这并不表示从客户端发送的值是 0 还是 null。可能是你可以有一个 setter 方法,可以调用它来初始化/设置客户端发送的值。然后你可以定义你的指标值,可能是一些负值来表示空值..
回答by Dead Programmer
I think you are asking about code like this.
我想你是在问这样的代码。
int count = (request.getParameter("counter") == null) ? 0 : Integer.parseInt(request.getParameter("counter"));
回答by Anonymous Person
An integer can't be null but there is a really simple way of doing what you want to do. Use an if-then statement in which you check the integer's value against all possible values.
整数不能为空,但有一种非常简单的方法可以做您想做的事情。使用 if-then 语句,根据所有可能的值检查整数的值。
Example:
例子:
int x;
//Some Code...
if (x<=0 || x>0){
//what you want the code to do if x has a value
} else {
//what you want the code to do if x has no value
}
Disclaimer: I am assuming that Java does not automatically set values of numbers to 0 if it doesn't see a value.
免责声明:我假设 Java 不会在没有看到值的情况下自动将数字的值设置为 0。
回答by user5432692
I think you can initialize the variables a value like -1
,
because if the int
type variables is not initialized it can't be used.
When you want to check if it is not the value you want you can check if it is -1
.
我认为您可以将变量初始化为一个值-1
,因为如果int
未初始化类型变量,则无法使用它。当您想检查它是否不是您想要的值时,您可以检查它是否是-1
.
回答by chaitanya
int cannot be null. If you are not assigning any value to int default value will be 0. If you want to check for null then make int as Integer in declaration. Then Integer object can be null. So, you can check where it is null or not. Even in javax bean validation you won't be able to get error for @NotNull annotation until the variable is declared as Integer.
int 不能为空。如果您没有为 int 分配任何值,则默认值将为 0。如果您想检查 null,则在声明中将 int 设为 Integer。那么 Integer 对象可以为空。因此,您可以检查它是否为空。即使在 javax bean 验证中,在将变量声明为 Integer 之前,您也不会收到 @NotNull 注释的错误。
回答by chaitanya
public class Demo {
private static int i;
private static Integer j;
private static int k = -1;
public static void main(String[] args) {
System.out.println(i+" "+j+" "+k);
}
}
OutPut: 0 null -1
输出:0 空 -1