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 Clarification about Ternary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification about Ternary operator" Watch "Clarification about Ternary operator" New topic
Author

Clarification about Ternary operator

Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Question 49 from http://www.valoxo.ch/jr/mocks/mock01a.html

Can anybody please tell how to interpret ternary operator in following Code? It prints "hello" as output.



Regards Balraj


Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
The ternary operator is <evaluation> ? <true result> : <false result>.
Your code shows three ternary operators. Ugly but valid.

What it is you don't understand? This seems simple enough. Just not that the condition is no "b==!b" but "b=!b", which means the result of "!b" will be assigned to "b" and then that value used in the evaluation.
So in the first "b=!b", the evaluation is "true".


SCJP6
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
<evaluation> ? <true result> : <false result>.

To add to what Jason said: b=!b is assigning the value of !b(which is true) to b, now b becomes true. After the first ternary there is nothing to do there and second ternary will assign the value of !b to b, keep in mind that b is true from the first ternary and now in the second b will become false. So when executing the second ternary it will print hello with lower initial letter h because b is false.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
This question highlights one of the issues with ternary operators. Whilst they may be convenient, when you nest them or they go wrong; they can be a royal pain.

On the job I stick to fully declared if/else with braces; unless I have good reason not to.
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Sorry to bother you guys again I could understand first half of it.
This is how I could interpret half first half of this condition



I do not know what happens to condition after "hello", why It did not execute.
It this the else part of first condition?

Regards Balraj
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Balraj Momi wrote:
I do not know what happens to condition after "hello", why It did not execute.
It this the else part of first condition?




[SCJP 6.0]
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Thanks a lot everyone, ternary operators can no longer bother me
Thanks again
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Balraj Momi wrote:Sorry to bother you guys again I could understand first half of it.
This is how I could interpret half first half of this condition





In this particular case, it is far to convoluted to use a ternary operator as you have nested statements. Here the use of the ternary is making things far to complicated.
One design recommendation in Java is to factor bigger complex tasks out into smaller easier ones. The ability to do this due to the scope and
flexibility of the Java language is one of its defining factors as one of the best languages out there (although some may argue the opposite).
SO in this case, it would be far better to re-factor the code to use the usual if-else. Of course you could do this anyway to understand it more, yet still
keep your final code as ternary. For me however, the easier the better!

See Anastasia's code post above, it really makes things simpler don't you think?



be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

See Anastasia's code post above, it really makes things simpler don't you think?


I agree with you, If we stick to normal "If else's" instead of Ternary Operator, It will make everyone's life (including us) life much easier.

Regards
Balraj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification about Ternary operator