Skip to content
Home » Blog » Java Program to compare two strings

Java Program to compare two strings

Program to compare two strings in Java

In this program, you’ll learn how to compare two strings in Java.

To properly understand this Program to compare two strings, you should have the knowledge of following Java programming topics:

  • java string

Below Program compares two strings by using str compareTo (string), str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ASCII difference of first odd characters of compared strings. java string documentation

Program to compare two strings

public class StringCompareEmp{
   public static void main(String args[]){
      String str = "Hello World";
      String anotherString = "hello world";
      Object objStr = str;

      System.out.println( str.compareTo(anotherString) );
      System.out.println( str.compareToIgnoreCase(anotherString) );
      System.out.println( str.compareTo(objStr.toString()));


Java Program to compare two strings

Follow @coderforevers on Instagram

Ask your questions about how to compare two string in Java and clarify your/others doubts by commenting. Documentation.

Please write to us at [email protected] to report any issue with the above content or for feedback.

More Programs in Java:

  1. Java Program to Convert String to Date
  2. Java Program to Get Current Date/time
  3. Java Program to Convert Milliseconds to Minutes and Seconds