为什么在 Scala Long 中不能像 Integer 那样初始化为 null [英] Why in Scala Long cannot in initialized to null whear as Integer can

查看:34
本文介绍了为什么在 Scala Long 中不能像 Integer 那样初始化为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我的 Scala bean,它是一个要从 YML 配置加载的配置.如果未指定,我希望 long 属性为 null,但我面临以下问题.知道为什么吗?

I am creating my Scala bean which is a configuration to be loaded from a YML config. I want a long property to be null if not specified, but I'm facing below issue. Any idea why?

startOffset: Integer = null
scala> var endOffset: Long = null
<console>:11: error: an expression of type Null is ineligible for implicit conversion
   var endOffset: Long = null
                         ^`

PS:是的,我可以使用 Option[Long] 但希望清楚,这种方法有什么问题吗.

PS: Yes I can use Option[Long] but wanted clarity and is there anything wrong with this approach.

推荐答案

Scala Long 是类似于 Java 中的 long 的文字数据类型.Int 也是如此.但是 Integer 是一个 Wrapper 类,即 java.lang.Integer

Scala Long is literal data type like long in Java. Same is true for Int. But Integer is a Wrapper class i.e java.lang.Integer

如果你想要可为空的 Long 值,你可以使用 java.lang.Long

If you want nullable Long value, you can use java.lang.Long

这篇关于为什么在 Scala Long 中不能像 Integer 那样初始化为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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