Is there a good way to do away with null values in java like in ruby there is:
x = null || "string"
I want to do somthing like this.
String line = reader.readLine() || "";
To prevent null values being passed.
You could use Guava and its method (Removed from Guava 21.0):Objects.firstNonNull
String line = Objects.firstNonNull(reader.readLine(), "");
Or for this specific case, Strings.nullToEmpty
:
String line = Strings.nullToEmpty(reader.readLine());
Of course both of those can be statically imported if you find yourself using them a lot.
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