0

I have a code

public int getValue()
{
    int i =  null;

if(condition)
{
i = 10;
}


    return i;
}

This works fine for String variables. How to do the same with int variables ?

8 Answers 8

5

int is a primitive type. It cannot hold null value.

You can either use Integer to hold null values, or use 0 (or -1) as the default int value.

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

1 Comment

I guess -1 is more common, but after all it depends on what that int represents.
3

null is a valid value for objects, not for primitives.

Since String instances are objects, this is why it compiles it this case.

To get your code compiling in the case with the int, just do:

int i;

if (condition) {
    i = 10;
} else {
    i = -1; //or some other value when the condition is not met.
}

1 Comment

You haven't really provided a solution to his problem.
2

Only Objects can hold a null value. Since int is a primitive type it has its own default value.

Objects default is null

Data Type   Default Value (for fields)
byte                     0
short                    0
**int**                  0
long                     0L
float                    0.0f
double                   0.0d
char                     '\u0000'
**String (or any object)**  null
boolean false

Try

  int i =  0;

or even left it and assign later, If it is a instance member. Remember that local variables need to be initialize before they are using at that place you have to assign.

Comments

1

The nullequivalent for int is 0.

3 Comments

This is wrong on so many levels. Integer null is the real equivalent.
Actually it really depends on the code - if you are searching for an index in a list you would like to return -1 as the "nullable" value. But an unassigned int holds 0 by default, so would you care to elaborate more? Thank you ^^
int always holds a valid value, your application interprets it whatever it wants/needs, so there's no real equivalent for null. i++ is ok on 0, -1... o.someThing() is not when o is null. There's a deep (conceptual and practical) difference! It reminds me of the "third state" of the three-state logic, but the analogy isn't perfect of course.
1

You can't put "null" as a value for primitive data type in java, but you can use Objects like "Integer":

Example:

public Integer getValue() {
     Integer i;
     return i = (condition ? null : 10);
}

The previous code will return null as Integer object if your condition is true, otherwise it will return 10.

But, commonly used to return -1 as default int value if conditions not matched, so you can use:

public int getValue() {
     int i = -1;
     return i = (condition ? -1 : 10);
}

Comments

1

You can use Integer instead of int .

public static Integer getValue()
    {
     Integer i =  null;

     if(condition)
     {
       i = 10;
     }
        return (i==null)?0:i;
    }

If you don't want to change int , then you can give

public static int getValue()
        {
         int i=0;

         if(condition)
         {
           i = 10;
         }

            return i;
        }

1 Comment

You should also return an Integer, returning a null value with int as return type will cause some problems. Also, the second snippet will not compile.
1

I use a sentinel value like MIN_VALUE

public int getValue() {
    int i =  Integer.MIN_VALUE;

    // do something
    if (i == Integer.MIN_VALUE) {
        i = 10;
    }

    return i;
}

However the simpler solution is to give an appropriate default value like 10

public int getValue() {
    int i =  10;

    // do something

    return i;
}

Comments

0

Primitives (int, long, byte, etc) doesn't have null values, that applies only to objects (and String is an object in Java). Also note that default value for primitives is usually 0, for objects is null. You have several options to overcome this

Throw an exception

public int getValue() {
   if(condition) {
       return 10;
   }
   throw new IllegalStateException("Condition must be met");  
}

Or you can return some arbitrary number which will tell you that condition wasn't met, -1 is a standard way.

public int getValue() {
    int value = -1;
    if(condition) {
        value = 10;
    }
    return value;
}

Also note that i is usually used within for loop, so I would prefer different name for that variable, it can be confusing otherwise.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.