Java String equals() method


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


if (a.equals(b)) { 
    //statement
}

Example: How to implement the String equals() method in java.

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("Enter second string:");
        b = sc.nextLine();

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

    }
}

Output:

Enter first string:xiith
Enter second string:xiith
Strings are equal