Wednesday 27 February 2013

24. What is displayed when the following code is compiled and executed?

24. What is displayed when the following code is compiled and executed?


Choices:
a. Same
Equals
b. Equals
c. Same
d. The code compiles, but nothing is displayed upon execution.
e. The code fails to compile.


Answer:-
B is correct.
Here s1 and s2 are two different object references, referring to different objects in memory.
Please note that operator == checks for the memory address of two object references being compared and not their value.
The "equals()" method of String class compares the values of two Strings.
Thus s1==s2 will return "false" while s1.equals(s2) will return "true".
Thus only "Equals" will be printed.

No comments:

Post a Comment