Java String equals() method


In this program, You will learn how to implement String equals() method in Java.


if (a.equals(b)) { }


Example: How to implement String equals() method in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String a, b;

        System.out.print("Enter First String :");
        a = sc.nextLine();

        System.out.print("\nEnter Second String :");
        b = sc.nextLine();

        if (a.equals(b)) {
            System.out.print("\nBoth Strings are equal");
        } else {
            System.out.print("\nBoth Strings are not equal");
        }

    }
}

Output:

Enter First String :dog
Enter Second String :dog
Both Strings are equal