[Java] Java null coalescing operator

less than 1 minute read

While thinking that it’s not good because there is no null coalescing operator in Java As soon as the Util function is prepared

Is it possible to use it with the same name as a SQL function with the name coalesce?

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 the ?? operator like C#.