Java

Java Program to Compare Two Strings Using equals()1 min read

In the below program, we have two strings style and style2 both containing the same world Bold.

However, we’ve used String constructor to create the strings. To compare these strings in Java, we need to use the equals() method of the string.

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.




On the other hand, equals() method compares whether the value of the strings are equal, and not the object itself.

If you instead change the program to use equality operator, you’ll get Not Equal as shown in the program below.

 

Output:

 

Leave a Comment