Is there more concise way to write:
if (myInteger != null && myInteger != 0) { ... }
For example, for Strings you can use StringUtils.isBlank()
int stores the values in the memory in the form of binary by default. It means they can't be null. Primitive data type int can not be checked for a null value.
null means that a variable contains a reference to a space in memory that does not contain an object. 0 is a numeric data type with a value of 0. Nothing doesn't really exist, however I think you may be viewing this as an empty String "" which is simply a String data type that does not contain a value.
To check if a string is null or empty in Java, use the == operator. Let's say we have the following strings. String myStr1 = "Jack Sparrow"; String myStr2 = ""; Let us check both the strings now whether they are null or empty.
With Java 8:
if (Optional.ofNullable(myInteger).orElse(0) != 0) { ... }
Note that Optional
may help you to completely avoid the if condition at all, depending on your use case...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With