1 What will be printed out on the console if sequalss Syst
1. What will be printed out on the console?
if ( \"s\".equals(\'s\') )
System.out.println(\"TRUE\");
else
System.out.println(\"FALSE\");
A) TRUE
B) FALSE
C) There\'s an error in the code
Enter your answer: A
The correct answer is: B
I\'m confused why the answer is false. Equals() compare the value, so s is s but why the answer is false?
Solution
Answer: FALSE
if ( \"s\".equals(\'s\') )
System.out.println(\"TRUE\");
else
System.out.println(\"FALSE\");
In condition, we applied equals() method on String and char. Equals() method will check the equalities between two objects and values of them. Since both values are different types String and char, If condition will return false.
if ( \"s\".equals(\"s\") )
System.out.println(\"TRUE\");
else
System.out.println(\"FALSE\");
This condition will return true becuase equals method applied on two String objects and both values are same so condition will return true.
Answer in this case is TRUE
