Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification about Ternary operator

 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everyone, ternary operators can no longer bother me
Thanks again
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic