Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

Write a Java program to compare two strings?

A String is a series of characters. In Java, String objects are immutable, which means they are constant and cannot be modified once created.

You can compare two Strings in Java using the compareTo() function, equals() function, or == operator.

Here is a Java program that compares two strings using the equals () method.

import java.util.Scanner;
public class CompareStrings
   public static void main(String[] args)
      String str1, str2;
      Scanner sc = new Scanner(;
      System.out.print("Enter your First String: ");
      str1 = sc.nextLine();
      System.out.print("Enter the Second String: ");
      str2 = sc.nextLine();
       System.out.println("Strings are equal.");
       System.out.println("Strings are not equal.");

Test your websites, web-apps, or mobile apps seamlessly with LambdaTest.

Start Free Testing

Earn resume-worthy Selenium certifications that help you land a top job.

Learn More

Test your web or mobile apps

Test orchestration and execution cloud of 3000+ browsers and OS


24/7 support


Enterprise grade security


Fastest test execution cloud