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 ?
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.
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.
}
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.
The nullequivalent for int is 0.
-1 as the "nullable" value. But an unassigned int holds 0 by default, so would you care to elaborate more? Thank you ^^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.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);
}
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;
}
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;
}
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.