8.2k views
1 vote
Which expression is not true?

1. "john" == "john";
2. "john".equals("john");
3. "john" = "john"
4. "john".equals(new String("john"))

User Halberdier
by
7.6k points

1 Answer

5 votes

Final answer:

Option 3, "john" = "john", is not true because it uses a single equals sign for comparison, which is incorrect in most programming languages. Instead, one should use either == or .equals() method to test equality of strings.

Step-by-step explanation:

The expression that is not true among the options provided is "john" = "john". This is because in many programming languages, including Java, the single equals sign (=) is used for assignment and not for testing equality. To test equality of strings, one should use either the double equals sign (==) or the .equals() method.

Option 1, "john" == "john", checks if the two string literals refer to the same object, which is typically true for string literals in Java due to string internment. Option 2, "john".equals("john"), checks if the two strings have the same value, which is true. Option 3 is incorrect as previously explained. Option 4, "john".equals(new String("john")), also checks for value equality and is true because .equals() compares the content of the strings rather than their reference.

User Arieltools
by
8.3k points