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.
readLine() can return null as well. You should just check for null when you need to use the String.
String line = reader.readLine() || ""; If readLine can return null, and if this construct is allowed, you still can be left with a null. I know Guava and Apache libraries are really good, but we are going to end up soon with applications built with 30 libraries sitting on top of each other. So I still think this is the best answer. Does he really need a 2MB "enterprise" library for this? You all decide.