An Integermaybe null. I convert Integerto intusing:
Integer
null
int
Integer integer = null; int i; try { i = integer.intValue(); } catch (NullPointerException e) { i = -1; }
Is there a better way?
Avoiding exclusion is always better.
int i = integer != null ? integer.intValue() : -1;
If you already have guavaon your class path, I like the answer provided by michaelgulak .
guava
Integer integer = null; int i = MoreObjects.firstNonNull(integer, -1);
Apache Commons Lang 3
ObjectUtils.firstNonNull(T...)
Java 8 Stream
Stream.of(T...).filter(Objects::nonNull).findFirst().orElse(null)
: fooobar.com/questions/50441/...
Java8 :
Optional.ofNullable(integer).orElse(-1)
Source: https://habr.com/ru/post/1612727/More articles:Laravel 5.1 Undefined variable: comments - phpAndroid library project depending on other library projects - androidHow to link dependency resources with my own aar? - android-gradleTrying to get a biased error property on Yii2 after adding Materialize css - cssInclude / libs / folder in aar - androidInclude folder in Gradle artifact - gradleWhich C ++ STL class should be used to reduce fragmentation caused by many small distributions? - c ++Testing an arbitrary dialect of Timeleaf with Mockito - javathe controller in the "shahab" directive does not work. (Null) - angularjsAlternative Android WebView - androidAll Articles