216k views
3 votes
If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal?

A) str1 = str2
B) str1 && str2
C) str1.equals(str2)
D) str1 += str2

User Nemenos
by
8.1k points

1 Answer

2 votes

Answer:

The correct expression to determine whether or not two String objects (str1 and str2) are equal is option C: str1.equals(str2)

Step-by-step explanation:

Option A (str1 = str2) assigns the reference of str2 to str1, which does not compare the contents of the two strings.

Option B (str1 && str2) is not a valid expression for comparing String objects, as it uses the logical AND operator instead of the equality operator.

Option D (str1 += str2) concatenates str2 to str1 and updates the value of str1, which also does not compare the contents of the two strings.

User Verhogen
by
7.4k points