简体   繁体   English

如何将null传递给期望long或int的方法?

[英]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传递给需要longint

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. 问题是intlong是原语。 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. 您当然可以在方法签名中使用包装类IntegerLong而不是longint

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.

相关问题 在Scala中,如何将null传递给期望Long的Java方法? - In Scala, how can I pass null to a Java method that expects Long? 如何将null返回给int方法 - how to return null to int method 用 int 和 long 重构方法 - Refactor method with int and long 王子类型中的Encrypt(long,long,long,long,int)方法不适用于参数(String,long,long,long,long,int) - The method Encrypt(long, long, long, long, int) in the type prince is not applicable for the arguments (String, long, long, long, int) 如何使用方法Object.wait(长时间超时,int nanos) - How to use method Object.wait(long timeout, int nanos) 为什么JVM允许将B []传递给期望A []的方法? - Why does the JVM allow be to pass a B[] to a method that expects an A[]? 如何将int值从一种方法传递给另一种方法 - How do I pass int values from one method to another 将原始int数组类(int []。class)传递给需要Class的通用方法 <T[]> 无法编译 - passing primitive int array class (int[].class) to a generic method which expects Class<T[]> fails to compile 更简单的自动装箱方法/在java中将Long转换为int - Simpler method to autobox / cast a Long to int in java 如何将属性文件中的数字参数传递给 Spring 注释编号(long、或 int、或 Integer 等)参数 - How to pass a number parameter from a properties file to a Spring annotation number (long, or int, or Integer, etc) parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM