- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Java String compareTo() method
You can compare Strings using compareTo() method or, equals() method or, or == operator. Following example demonstrates how to initialize and compare strings in Java.
Example
public class StringDemo { public static void main(String[] args) { String str1 = "tutorials"; String str2 = "point"; // comparing str1 and str2 int retval = str1.compareTo(str2); // prints the return value of the comparison if (retval < 0) { System.out.println("str1 is greater than str2"); } else if (retval == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is less than str2"); } } }
Output
str1 is less than str2
- Related Articles
- Java String compareTo() Method example.
- String compare by compareTo() method in Java
- Java Program to compare string using compareTo() method
- Java Integer compareTo() method
- LocalTime compareTo() method in Java
- MonthDay compareTo() method in Java
- LocalDateTime compareTo() method in Java
- Instant compareTo() method in Java
- Duration compareTo() method in Java
- IntBuffer compareTo() method in Java
- FloatBuffer compareTo() method in Java
- DoubleBuffer compareTo() method in Java
- ShortBuffer compareTo() method in Java
- ByteBuffer compareTo() method in Java
- CharBuffer compareTo() method in Java

Advertisements