Java - Write code to return default value If a string is Empty

Requirements

Write code to return default value If a string is Empty

Both null and "" are empty string

Hint

Use Conditional operators.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "";
        String defaultStr = "book2s.com";
        System.out.println(defaultIfEmpty(str, defaultStr));
    }/* ww w .  j a v a 2 s. co m*/

    public static String defaultIfEmpty(String str, String defaultStr) {
        return isEmpty(str) ? defaultStr : str;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

Related Exercise