Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

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.in);
      
      System.out.print("Enter your First String: ");
      str1 = sc.nextLine();
      System.out.print("Enter the Second String: ");
      str2 = sc.nextLine();
      
      if(str1.equals(str2)) 
       System.out.println("Strings are equal.");
      else
       System.out.println("Strings are not equal.");
   }
}

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free