αƞjiβ αƞjiβ - 2 months ago 7
Scala Question

Checking null value in Scala for Java data type

I have call in Scala code which give Java

Double
type which can be
null
how can I check between null and non-null value in Scala code?

val myJavaDoubleValue: Double = thirdpartyCall.collectValue()
if(myJavaDoubleValue == null) {
println("Is NULL")
} else {
println("Is NOT NULL")
}


Any suggestion on doing this in Scala way?

Answer

Use an Option:

val myJavaDoubleValue: Option[Double] = Option(thirdpartyCall.collectValue())

The above will result in None if the result is null, otherwise in Some(actualValue).

See Option.apply()