0

I'm trying to create my own "int compareTo(String,String)" method. I was wondering what should I return if I find any of the strings are null? throw an exception? return -1 is a possible result for compareTo...

public int compare(String st1, String st2){
    if (st1 == null) || st2 == null ) return ..
    ...
    //logic
    ...
}

compareTo definition:

public int compareTo(String anotherString)

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)

dReAmEr
  • 6,986
  • 7
  • 36
  • 63
Maciano
  • 3
  • 2

3 Answers3

1

It's up to you to decide, as long as you respect the contract of Comparator.

You can throw an NullPointerException. That means that you consider it a bug to pass null to your comparator.

You can choose to make null smaller than any non-null string. In that case, compare(null, "nonNull") must return a negative integer, compare("nonNull", null) must return a positive integer, and compare(null, null) must return 0.

Or you can choose to make null bigger than any non-null string. In that case, compare(null, "nonNull") must return a positive integer, compare("nonNull", null) must return a negative integer, and compare(null, null) must return 0.

Returning -1 if any of the argument is null is a violation of the contract: it means that null is both smaller and bigger than a non-null string, depending on the order of the arguments.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

The JDK precedent here is to throw a NullPointerException, which is generally a reasonable approach, as nulls often represent errors that its good to fail fast on.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

The best would be following the Comparable specification:

Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Community
  • 1
  • 1
  • 1
    The question is about Comparator, not Comparable. Although Comparable must throw a NPE in case of null to fulfill the contract, that's not the case for Comparator. BTW, its javadoc says: *Unlike Comparable, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation*. – JB Nizet Jan 19 '15 at 19:28
  • `int compareTo(String)` from String class is due to Comparable interface implementation. The method defined by Comparator is `int compare(Object, Object)`. – Bruno Marco Visioli Jan 19 '15 at 19:46
  • Err yes? So what? The question starts with: "I'm trying to create my own "int compareTo(String,String)". So the OP is creating a Comparator. He obviously can't override String's `compareTo()` method, since String is final. – JB Nizet Jan 19 '15 at 19:49