- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String Comparison in Java
You can compare two Strings in Java using the compareTo() method, equals() method or == operator.
The compareTo() method compares two strings. 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.
The equals() method of the String class compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
You can also compare two strings using == operator. But, it compares references of the given variables not values.
- Java String Comparison Methods.
- Case sensitive string comparison in Java.
- Java string comparison sample code examples
- Java String comparison, differences between ==, equals, matches, compareTo().
- Implementing String Comparison in MongoDB?
- Case-insensitive string comparison in C++
- How do we make my string comparison case insensitive in java?
- Comparison of Float in Java
- Perform element-wise comparison of two string arrays using a comparison operator in Numpy
- Comparison of Java and .NET
- Comparison of autoboxed integer object in Java
- How MySQL can perform case-sensitive string comparison?
- Comparison of Exception Handling in C++ and Java
- How to make SQL case sensitive string comparison in MySQL?