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

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

问题描述

我正在创建我的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是包装器类,即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 whear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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