
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Compare two Strings in Java
Compare two strings using compareTo() method in Java. The syntax is as follows −
int compareTo(Object o)
Here, o is the object to be compared.
The return value is 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.
Example
Let us now see an example −
public class Demo { public static void main(String args[]) { String str1 = "Strings are immutable"; String str2 = new String("Strings are immutable"); String str3 = new String("Integers are not immutable"); int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); } }
Output
0 10
Let us see another example wherein we are comparing two strings lexicographically, ignoring case differences using compareToIgnoreCase(). This method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.
The syntax is as follows −
int compareToIgnoreCase(String str)
Here, str is the string to be compared.
Example
Let us now see an example to compare strings, ignoring case −
public class Demo { public static void main(String args[]) { String str1 = "Strings are immutable"; String str2 = "Strings are immutable"; String str3 = "Integers are not immutable"; int result = str1.compareToIgnoreCase( str2 ); System.out.println(result); result = str2.compareToIgnoreCase( str3 ); System.out.println(result); result = str3.compareToIgnoreCase( str1 ); System.out.println(result); } }
Output
0 10 -10
- Related Articles
- Compare two strings lexicographically in Java.
- Compare two Strings lexicographically in Java programming
- Compare the two Strings lexicographically in Java
- Program to Compare two strings in Java
- Java Program to Compare Two Strings
- Java Program to Compare two strings lexicographically
- How to compare two strings without case sensitive in Java
- Compare two strings lexicographically in C#
- How to compare strings in Java?
- How to compare two strings in Golang?
- Java Program to Compare Strings
- Golang program to compare two strings
- How do I compare strings in Java?
- C++ Program to Compare two strings lexicographically
- Golang program to compare two strings lexicographically
