top of page
Writer's pictureThe Tech Platform

Write a Program to Check whether the two Strings are Equal or Not in Java

An array is a data structure in java that holds values of the same type with its length specified right from creation time. Think of a container, like the creation of eggs.


In the following example, we defined two strings, and then used String.equals() method. If two string are equal, which is an yes in the below example, equals() method returns true, else false.


Code:

import java.util.Scanner;
 
/**
 * An example java program to compare if two strings are equal, with inputs read from console
 */
public class CheckStringsEqual {
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System. in);
        //read first string
        System.out.print("Enter first string : ");
        String str1 = scanner.nextLine();
         
        //read second string
        System.out.print("Enter second string : ");
        String str2 = scanner.nextLine();
         
        //check if two strings are equal
        boolean areTwoStringsEqual = str1.equals(str2);
 
        System.out.print("Two strings are equal : "+areTwoStringsEqual);
    }
}

Output:

In this we have two string "Str1 : The Tech Platform" and "Str2: The Tech Platform"

So the answer will be true. As both strings are same







In this We have "Str1: Good Morning" and "Str2: Good morning".

The answer will be false because in the second string "morning" is different from the first string "Morning".








Run the code to see the results.



The Tech Platform




0 comments

Recent Posts

See All

Comments


bottom of page