A difference is that "foo".equals((String)null)
returns false while "foo".compareTo((String)null) == 0
throws a NullPointerException. So they are not always interchangeable even for Strings.
More Related Contents:
- Java comparison with == of two strings is false? [duplicate]
- Difference between null and empty (“”) Java String
- What makes reference comparison (==) work for some strings in Java?
- How to check if my string is equal to null?
- BigDecimal equals() versus compareTo()
- String comparison and String interning in Java
- String equals and == with String concatenation [duplicate]
- Demonstrating string comparison with Java
- Java: splitting a comma-separated string but ignoring commas in quotes
- How do I convert from int to String?
- String’s Maximum length in Java – calling length() method
- How to convert comma-separated String to List?
- Any reason to prefer getClass() over instanceof when generating .equals()?
- Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
- String Constant Pool
- How to split a comma-separated string?
- Java, Calculate the number of days between two dates [duplicate]
- Should I use string.isEmpty() or “”.equals(string)?
- Java – removing first character of a string
- Fatal Error: Invalid Layout of java.lang.String at value
- How to print the whole String pool?
- How do I convert a byte array with null terminating character to a String in Java?
- What is the difference between String and StringBuffer in Java?
- How to remove large if-else-if chain [duplicate]
- Java – checking if parseInt throws exception
- Find all numbers in the String [closed]
- How to parse this string in Java?
- Is it possible in Java to override ‘toString’ for an Objects array?
- Parse Date from String in this format : dd/MM/yyyy [to dd/MM/yyyy]
- How many Java objects are generated by this – new String(“abcd”)