Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java String compareTo()

Java String compareTo() method example. Learn to compare two strings lexicographically. We can consider this string comparison dictionary based comparison.

Lokesh Gupta

January 11, 2023

Java String class
Java Compare, Java String
Java String

Java String.compareTo() compares two strings lexicographically (in dictionary order) in a case-sensitive manner. For case-insensitive comparison, use compareToIgnoreCase() method.

1. Strings in Lexicographic Order

If a string 'string1' comes before another string 'string2' in the dictionary, then string2 is said to be greater than 'string1' in string comparison.

  • string1 > string2 – ‘string1’ comes AFTER ‘string2’ in dictionary.
  • string1 < string2 – ‘string1’ comes BEFORE ‘string2’ in dictionary.
  • string1 = string2 – ‘string1’ and ‘string2’ are equal.

2. String.compareTo() API

The compareTo() function takes one argument of type String. The second string is the String object itself, on which method is called. Note that compareTo() does the string comparison based on the Unicode value of each character in the strings.

int result = string1.compareTo(string2);

The result of this method is in integer value where –

  • positive integer – string1 lexicographically follows string2.
  • negative integer – string1 lexicographically preceds string2.
  • zero – both strings are equal.

3. Java Program to Compare Strings

The following Java program compares two strings case-sensitively using the compareTo() method. We ae using Hamcrest matches to assert the return values.

String name = "alex";

//same string
assertThat(name.compareTo("alex"), equalTo(0));

//Different cases
assertThat(name.compareTo("Alex"), greaterThan(0));
assertThat(name.compareTo("ALEX"), greaterThan(0));

//Different strings
assertThat(name.compareTo("alexa"), lessThan(0));
assertThat(name.compareTo("ale"), greaterThan(0));

4. Difference between compareTo() and equals()

The differences between compareTo() and equals() methods are:

  • The compare() compares lexicographically (dictionary ordering), and equals() checks for the content of both strings, although both methods are case-sensitive.
  • The return type of compareTo() is an integer type concluding that string that is greater than, less than or equal to another string. The return type of equals() is boolean, meaning the content of both strings is equal or not.

Happy Learning !!

Reference: String Java Doc

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

Table of Contents

  • 1. Strings in Lexicographic Order
  • 2. String.compareTo() API
  • 3. Java Program to Compare Strings
  • 4. Difference between compareTo() and equals()
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String equalsIgnoreCase()

Next

Java String compareToIgnoreCase()

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap