...
Code Block | ||
---|---|---|
| ||
boolean isCapitalized(String s) { if (s == null) { throw new NullPointerException(); } if (s.equals("")) { return true; } String first = s.substring(0, 1); String rest = s.substring(1); return (first.equals(first.toUpperCase()) && rest.equals(rest.toLowerCase())); } |
Actually Note that the null check is redundant, ; if the null check it were removed, then the next call (s.equals("")
) would throw a NullPointerException
if s
was were null. However, the explicit null check is good form, as because it explicitly signifies indicates the codeprogrammer's intentionintent. More complex code may require explicit testing of invariants and appropriate throw statements.
...