16

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.

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.

Any suggestions?

5
  • “that variable comes from a browser with a null value” – What do you mean by that? as pointed out by Bozho, this is clearly impossible. Commented Sep 29, 2010 at 11:02
  • @Konrad Rudolph: You down voted for this: “that variable comes from a browser with a null value”. I solved my problem with the help of Bozho's answer "then it is the converter which decides whether to set 0" Yea, Int cannot be null & its impossible. But a converter can change the null value into 0 to assign it to a int variable. Now tell me why you down voted. Whats the wrong with my question. I am clearly starts with "I really dont know this is possible or not". What does it mean!!! I really dont know b4 tht... Think twice b4 down voting Commented Sep 29, 2010 at 11:26
  • I didn’t downvote. Your question may have been valid (I can’t tell, but I don’t downvote when I can’t) – I was just asking for clarifications. Commented Sep 29, 2010 at 11:29
  • @Konrad Rudolph: Ahhhhh..... Really sorry man... Sorry for that ok... But the comment remains. So, the one who down voted will know my explanation. Again sorry!! Commented Sep 29, 2010 at 11:33
  • Downvote: the situation you describe is impossible. Further information might clarify. Commented Sep 29, 2010 at 13:16

9 Answers 9

42

int variables can't be 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.

Sign up to request clarification or add additional context in comments.

1 Comment

What about int?? Is there a function similar to string.IsNullOrEmpty()?
5

I think you are asking about code like this.

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

Comments

5

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.

Comments

2

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

Comments

1
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

Comments

0

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.

Comments

0

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.

1 Comment

This is really a comment, not an answer. With a bit more rep, you will be able to post comments.
0

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()

Comments

-2

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.

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.

1 Comment

Fields have default value of 0; local variables must be assigned a value or the code will not compile. That condition will be always true for any valid code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.