如何将null传递给期望long或int的方法? [英] How to pass null to method that expects long or int?

查看:111
本文介绍了如何将null传递给期望long或int的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个愚蠢的问题,但我如何将 null 传递给 long 或<$ c $的方法c> int ?

Could be dumb question, but how can I pass null to method which takes long or int?

示例:

TestClass{
  public void iTakeLong(long id);
  public void iTakeInt(int id);
}

现在如何将null传递给两种方法:

now how can i pass null to both methos:

TestClass testClass = new TestClass();
testClass.iTakeLong(null); // Compilation error : expected long, got null
testClass.iTakeInt(null);   // Compilation error : expected int, got null

想法,建议?

推荐答案

问题是 int long 是原始人。您不能将 null 传递给原始值。

The problem is that int and long are primitives. You can't pass null to a primitive value.

您当然可以使用包装类 Integer Long 而不是 long int 在您的方法签名中。

You can certainly use the wrapper classes Integer and Long instead of long and int in your method signature.

这篇关于如何将null传递给期望long或int的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆