简体   繁体   English

如何检查一个 int 不是 null 还是空的?

[英]How to check whether a int is not null or empty?

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.实际上,变量来自具有null值的浏览器。 I cannot change the int declaration to Integer .我无法将int声明更改为Integer Because it has it own consequences.因为它有它自己的后果。

Any suggestions?有什么建议么?

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 ).如果要将null转换为int ,则由转换器决定是设置0 、抛出异常还是设置另一个值(如Integer.MIN_VALUE )。 Try to plug your own converter.尝试插入您自己的转换器。

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.我认为您可以将变量初始化为-1类的值,因为如果未初始化int类型变量,则无法使用它。 When you want to check if it is not the value you want you can check if it is -1 .当您想检查它是否不是您想要的值时,您可以检查它是否为-1

我想你是在问这样的代码。

int  count = (request.getParameter("counter") == null) ? 0 : Integer.parseInt(request.getParameter("counter"));

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.如果您的 int 变量被声明为类级别变量(实例变量),它将默认为 0。但这并不表示从客户端发送的值是 0 还是 null。 may be you could have a setter method which could be called to initialize/set the value sent by the client.可能是你可以有一个 setter 方法,可以调用它来初始化/设置客户端发送的值。 then you can define your indicator value , may be a some negative value to indicate the null..然后你可以定义你的指标值,可能是一些负值来表示空值..

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

Possibly browser returns String representation of some integer value?浏览器可能返回某个整数值的字符串表示? Actually int can't be null.实际上 int 不能为空。 May be you could check for null, if value is not null, then transform String representation to int.可能您可以检查 null,如果 value 不为 null,则将 String 表示形式转换为 int。

int cannot be null. int 不能为空。 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.如果您没有为 int 分配任何值,则默认值将为 0。如果您想检查 null,则在声明中将 int 设为 Integer。 Then Integer object can be null.那么 Integer 对象可以为空。 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.即使在 javax bean 验证中,在将变量声明为 Integer 之前,您也不会收到 @NotNull 注释的错误。

If you are receiving integer input, I would suggest checking if the text itself isn't empty, and then making a variable equal to it.toInt()如果您收到 integer 输入,我建议检查文本本身是否不为空,然后创建一个等于 it.toInt() 的变量

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM