| Author |
Operators
|
Swati Thorve
Ranch Hand
Joined: Jan 04, 2006
Posts: 43
|
|
Can anybody tell me the order of evaluation for string s public class Question49 { public static void main(String[] args) { boolean b = false; String s = (b=!b)?(b=!b)?"Hello":"hello": ( b=!b)?"world":"World"; System.out.println(s); } } The options are A.Prints: Hello B. Prints: hello C. Prints: Helloworld D. Prints: helloWorld E. Compilation error. Correct answer is B Plzz explain this. [ January 05, 2006: Message edited by: Swati Thorve ] [ January 05, 2006: Message edited by: Swati Thorve ] [ January 05, 2006: Message edited by: Swati Thorve ]
|
Thanks,<br />Swati Thorve<br />SCJP 1.4
|
 |
Mehul Sanghvi
Ranch Hand
Joined: Feb 04, 2002
Posts: 134
|
|
String s = (b=!b)?(b=!b)?"Hello":"hello": ( b=!b)?"world":"World"; The first (b=!b) is evaluated to true and hence the control comes to second (b=!b) which will now be evaluated to false and hence the value of "hello" will be assigned to String s. A possible catch here is = vs. == The expn (b=!b) is actually an assignment operation rather then a conditional statement (which is generally expected in ternary operators). Regards.
|
 |
Thomas De Vos
stable boy
Ranch Hand
Joined: Apr 12, 2003
Posts: 425
|
|
Let's play compiler here ... 1. I'm adding brackets 2. I'm adding more brackets 3. I'm assigning !b to b 4. I'm evaluating the first condition ... 5. I'm assigning !b to b again 6. Got my result
|
Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
|
 |
Swati Thorve
Ranch Hand
Joined: Jan 04, 2006
Posts: 43
|
|
|
Thanks a lot Mehul
|
 |
Swati Thorve
Ranch Hand
Joined: Jan 04, 2006
Posts: 43
|
|
|
Thanks a lot Thomas,For such a wonderful explanation.Thank u very much
|
 |
 |
|
|
subject: Operators
|
|
|