- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I compare strings 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.
- Related Articles
- How to compare strings in Java?
- In how many ways can I compare Strings in Java explain with example?
- Compare two Strings in Java
- How to Initialize and Compare Strings in Java?
- Compare two strings lexicographically in Java.
- Java Program to Compare Strings
- How does the Java compareTo() method, compare strings?
- How do we compare String in Java
- Compare two Strings lexicographically in Java programming
- Compare the two Strings lexicographically in Java
- Program to Compare two strings in Java
- How to compare two strings without case sensitive in Java
- Java Program to Compare Two Strings
- How do I compare String and Boolean in JavaScript?
- How do we compare two arrays in Java?

Advertisements