如何在java中检查Long for null

Bro*_*der 32 java null long-integer

如何在Java中检查null的Long值?

这会有用吗?

if ( longValue == null) { return blah; }
Run Code Online (Sandbox Code Playgroud)

brs*_*o05 77

原始数据类型不能null.只有Object数据类型可以null.

int,long等等......不可能null.

如果您使用Long(包装类long),那么您可以检查null's:

Long longValue = null;

if(longValue == null)
Run Code Online (Sandbox Code Playgroud)


man*_*uti 9

如果longValue变量是类型Long(包装类,而不是基元long),那么是的,您可以检查空值.

原始变量需要显式初始化为某个值(例如to 0),因此其值永远不会为null.


小智 7

您可以使用 来检查 Long 对象的空值longValue == null,也可以 longValue == 0L用于 long(原始),因为 long 的默认值为 0L,但如果 longValue 也为零,则结果将为 true


Dia*_*blo 7

如果它是Long对象,那么您可以使用longValue == null或者您可以Objects.isNull(longValue)在Java 8中使用 方法.

请查看对象以获取更多信息.