String Object Comparison in Java
Consider the following code segment.
public class StringCheck {
public static void main(String[] args) {
String fruit = new String("apple");
if(fruit == "apple")
System.out.println("They match!");
else
System.out.println("They do not match!");
}
}
The program produces the output They do not match!. Which of the following statements best explains this result?
A
Creating the String object with new instead of a string literal.
B
Omitting parentheses around the if condition.
C
Using “==” instead of the equals() method for string comparison.
D
Misspelling the string literal.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | ahsan48105 | 2 | 2 | 0m 00s | 200 |
| #2 | the.ronzono | 1 | 1 | 0m 00s | 100 |
| #3 | yekeon0515 | 0 | 1 | 0m 00s | -10 |
| #4 | psak12 | 0 | 1 | 0m 33s | -43 |
Items per page:
10
1 – 4 of 4
APFIVE