java

Java Program to Compare two Strings!

In this post, we are going to see how to compare two strings using Java. There are multiple ways we can compare strings in Java. I am going to show you the most commonly used ways here.

1. First method ( using equals() ) – Recommended: 

The output of the above program will be:

 

2. Second method ( using == ): 

The output of the above program will be:

Let’s write another example Java program to get the strings from the user and then compare the strings. 

We are going to use the Java Scanner to get the input from the user, so let’s see how we can do it.

Read the inline comments in the above Java Program to understand each and every line.

Here is another example using BufferReader:

Enjoy the day!

Related Posts

Leave a Reply