Java - Write code to compare two string values for equal and handle null values

Requirements

Write code to compare two string values for equal and handle null values

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str1 = "book2s.com";
        String str2 = "book2s.com";
        System.out.println(bothEmptyOrEquals(str1, str2));
    }/*from ww w.j a va2  s.co  m*/

    public static boolean bothEmptyOrEquals(String str1, String str2) {
        if (isEmpty(str1) && isEmpty(str2)) {
            return true;
        }
        return (isEmpty(str1) ? "" : str1)
                .equals(isEmpty(str2) ? "" : str2);
    }

    public static boolean isEmpty(String raw) {
        if (null == raw || raw.trim().length() == 0
                || "null".equals(raw.trim())) {
            return true;
        }
        return false;
    }
}

Related Exercise