I have very little knowledge on Groovy. I am using Groovy 2.4.12
.
I would like to do null
check on a variable which can be primitive or object. So I am trying to use Optional
utility to do null check. I am getting Optional can not be resolved
.
if(Optional.ofNullable(${value}).orElse(null) != null) {};
${value}
will be mapped from java code.
Optional
was introduced in Java 1.8 ( https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html )
Groovy 2.4.12 uses Java 1.7 (here's the matric http://groovy-lang.org/download.html#requirements )
If you have Java 1.8+ installed, Groovy should be able to use Optional
, provided it java.util.*
or java.util.Optional
was imported.
With the code sample you provided it is unclear what you're actually trying to accomplish, but, strictly speaking, Optional
does not save you from checking for null
, you just do it differently.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.