[英]java put null in int variable
I have a code 我有一个密码
public int getValue()
{
int i = null;
if(condition)
{
i = 10;
}
return i;
}
This works fine for String variables. 这对于String变量来说效果很好。 How to do the same with int variables ?
如何对int变量做同样的事情?
int
is a primitive type. int
是原始类型。 It cannot hold null
value. 它不能包含
null
值。
You can either use Integer
to hold null
values, or use 0
(or -1
) as the default int
value. 您可以使用
Integer
来保存null
值,也可以使用0
(或-1
)作为默认的int
值。
null
is a valid value for objects, not for primitives. null
是对象的有效值,不是基元的有效值。
Since String
instances are objects, this is why it compiles it this case. 由于
String
实例是对象,因此这就是在这种情况下对其进行编译的原因。
To get your code compiling in the case with the int
, just do: 要使用
int
代码编译,只需执行以下操作:
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. 只有对象可以包含
null
值。 Since int is a primitive type
it has its own default value. 由于int是
primitive type
因此它具有自己的默认值。
Objects default is null
对象默认为
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.
请记住,局部变量需要在要分配的位置使用之前进行初始化。
int
的null
等效值为0
。
You can use Integer
instead of int
. 您可以使用
Integer
而不是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 如果您不想更改
int
,则可以给
public static int getValue()
{
int i=0;
if(condition)
{
i = 10;
}
return i;
}
You can't put "null" as a value for primitive data type in java, but you can use Objects like "Integer": 您不能在Java中将“ null”作为原始数据类型的值,但可以使用“ 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. 如果您的条件为true,则前面的代码将返回null作为Integer对象,否则返回10。
But, commonly used to return -1 as default int value if conditions not matched, so you can use: 但是,如果条件不匹配,通常用于返回-1作为默认int值,因此可以使用:
public int getValue() {
int i = -1;
return i = (condition ? -1 : 10);
}
I use a sentinel value like MIN_VALUE 我使用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 但是,更简单的解决方案是提供适当的默认值,例如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). 基元(
int
, long
, byte
等)没有空值,仅适用于对象(而String
是Java中的对象)。 Also note that default value for primitives is usually 0
, for objects is null
. 另请注意,图元的默认值通常为
0
,而对象为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. 或者,您可以返回一些任意数字,这将告诉您不满足条件,
-1
是标准方式。
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. 另请注意,
i
通常在for循环内使用,因此我希望为该变量使用不同的名称,否则可能会造成混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.