8.3k views
0 votes
Assume that str1 and str2 are string class objects. Write code that displays "They are the same!" if the two objects contain the same string.

User CDZ
by
9.0k points

1 Answer

3 votes

Final answer:

To determine if two string objects contain the same string, use the .equals() method in Java.

Step-by-step explanation:

To determine if two string objects contain the same string, we can use the equals() method in Java. Here's an example:

if(str1.equals(str2)) {
System.out.println("They are the same!");
}

This code compares the content of str1 and str2 and if they are the same, it prints "They are the same!". If they are different, nothing happens.

User Kirk Ouimet
by
7.7k points