Below is a fairly standard piece of Java code which has been translated to Scala
val result: ResultSet = statement.executeQuery(query)
while (result.next) {
if (result.getDouble("FIELD") != null) {
}
}
Why do I get
warning: comparing values of types Double and Null using `!=’ will
always yield true?
If I understand correctly, it’s because a Double in scala can never be Null.
Trying something simple out in the console came up with errors:
On the other hand, a
java.lang.Doublecan be null:So your double is probably a Scala
Doubleas opposed to a JavaDoubleEDIT I poked around a bit, and it looks like using java.sql.ResultSet’s
getDoublemethod will return a scalaDoubleobject, which accounts for the warning. As for an alternative to checking fornull, I’m not sure that was ever an option. In Java, the method returned a primitivedouble, which would default to0.0. I may be wrong, but I don’t think Java primitives could ever benull.Edit2 Removed some stuff that was more speculation than answer.