[英]How to pass null to method that expects long or int?
Could be dumb question, but how can I pass null
to method which takes long
or int
? 可能是愚蠢的问题,但我如何将
null
传递给需要long
或int
?
Example: 例:
TestClass{
public void iTakeLong(long id);
public void iTakeInt(int id);
}
now how can i pass null to both methos: 现在我如何将null传递给两个方法:
TestClass testClass = new TestClass();
testClass.iTakeLong(null); // Compilation error : expected long, got null
testClass.iTakeInt(null); // Compilation error : expected int, got null
Thoughts, suggestions? 想法,建议?
The problem is that int
and long
are primitives. 问题是
int
和long
是原语。 You can't pass null
to a primitive value. 您不能将
null
传递给原始值。
You can certainly use the wrapper classes Integer
and Long
instead of long
and int
in your method signature. 您当然可以在方法签名中使用包装类
Integer
和Long
而不是long
和int
。
You can't - there's no such value. 你不能 - 没有这样的价值。 If you can change the method signature, you can make it take a reference type instead though.
如果您可以更改方法签名,则可以改为使用引用类型。 Java provides an immutable "wrapper" class for each primitive class:
Java为每个基本类提供了一个不可变的“包装器”类:
class TestClass {
public void iTakeLong(Long id);
public void iTakeInt(Integer id);
}
Now you can pass a null reference or a reference to an instance of the wrapper type. 现在,您可以将null引用或引用传递给包装器类型的实例。 Autoboxing will allow you to write:
Autoboxing将允许您写:
iTakeInt(5);
Within the method, you can write: 在方法中,您可以编写:
if (id != null) {
doSomethingWith(id.intValue());
}
or use automatic unboxing: 或使用自动拆箱:
if (id != null) {
doSomethingWith(id); // Equivalent to the code above
}
You can cast null to the non-primitive wrapper class, which will compile. 您可以将null转换为非原始包装类,它将进行编译。
TestClass testClass = new TestClass();
testClass.iTakeLong( (Long)null); // Compiles
testClass.iTakeInt( (Integer)null); // Compiles
But, this will throw a NullPointerException
when executed. 但是,这将在执行时抛出
NullPointerException
。 Not much help, but it is useful to know that you can pass the wrapper equivalent to a method that takes a primitive as an argument. 没有多大帮助,但知道你可以传递相当于一个以原语作为参数的方法的包装器是很有用的。
Depending on how many such methods you have, and how many calls, you have another choice. 根据您拥有的此类方法数量以及呼叫次数,您可以选择其他方式。
Instead of distributing null checks throughout your codebase, you can write wrapper methods (NB, not the type wrappers (int => Integer), but methods which wrap yours): 您可以编写包装器方法 (NB,不是类型包装器(int => Integer),而是包装您的方法),而不是在整个代码库中分配空检查:
public void iTakeLong(Long val) {
if (val == null) {
// Do whatever is appropriate here... throwing an exception would work
} else {
iTakeLong(val.longValue());
}
}
Use the Wrapper classes: 使用Wrapper类:
TestClass{
public void iTakeLong(Long id);
public void iTakeInt(Integer id);
public void iTakeLong(long id);
public void iTakeInt(int id);
}
You cannot do that. 你不能这样做。 Primitive types can't be
null
in Java. Java中的原始类型不能为
null
。
If you want to pass null
you have to change your method signature to 如果要传递
null
,则必须将方法签名更改为
public void iTakeLong(Long id);
public void iTakeInt(Integer id);
Typecasting the value to Long
as below will make the compilation error to disappear but eventually will end up in NullPointerException
. 如下所示将值转换为
Long
将使编译错误消失但最终将以NullPointerException
结束。
testClass.iTakeLong((Long)null)
One solution is to use type Long
instead of primitive long
. 一种解决方案是使用
Long
类型而不是long
。
public void iTakeLong(Long param) { }
Other solution is to use the org.apache.commons.lang3.math.NumberUtils
其他解决方案是使用
org.apache.commons.lang3.math.NumberUtils
testClass.iTakeLong(NumberUtils.toLong(null))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.