I'm thinking it's not cool because Java doesn't have a null coalescing operator As soon as the Util function is prepared
I wonder if it can be used with the name coalesce
as if it were a SQL function.
public final class Utils {
@SafeVarargs
public static <T> T coalesce(T... value) {
for (T v : value) {
if (v != null) {
return v;
}
}
return null;
}
}
public static void main() {
String foo = null;
String bar = null;
String hoge = Utils.coalesce(foo, bar, "huga");
System.out.println(hoge);
}
But to be honest, I want to use a ??
operator like C #.
Recommended Posts