File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operators" Watch "Operators" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operators