w3resource

Java: Check whether two String objects contain the same data

Java String: Exercise-13 with Solution

Write a Java program to check whether two String objects contain the same data.

Visual Presentation:

Java String Exercises: Check whether two String objects contain the same data

Sample Solution:

Java Code:

// Define a public class named Exercise13.
public class Exercise13 {
    // Define the main method.
    public static void main(String[] args) {
        // Declare and initialize three string variables.
        String columnist1 = "Stephen Edwin King";
        String columnist2 = "Walter Winchell";
        String columnist3 = "Mike Royko";

        // Check if columnist1 is equal to columnist2.
        boolean equals1 = columnist1.equals(columnist2);
        
        // Check if columnist1 is equal to columnist3.
        boolean equals2 = columnist1.equals(columnist3);

        // Display the results of the equality checks.
        System.out.println("\"" + columnist1 + "\" equals \"" +
            columnist2 + "\"? " + equals1);
        System.out.println("\"" + columnist1 + "\" equals \"" +
            columnist3 + "\"? " + equals2);
    }
}

Sample Output:

"Stephen Edwin King" equals "Walter Winchell"? false                                                          
"Stephen Edwin King" equals "Mike Royko"? false

Flowchart:

Flowchart: Java String  Exercises - Check whether two String objects contain the same data

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to check whether a given string ends with the contents of another string.
Next: Write a Java program to compare a given string to another string, ignoring case considerations.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/string/java-string-exercise-13.php