wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ternary Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ternary Operator " Watch "Ternary Operator " New topic
Author

Ternary Operator

ali bergen
Greenhorn

Joined: Apr 16, 2011
Posts: 5


Hi everybody,

Here is a question from ternary operator section.

public class Test {
public static void main(String... parans) {
System.out.println(true?false?"7":false?"6":false?true?"5": true?"4":"3":"2":"1");
}
}

Could anybody explain why the output is 2.
Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40

First step is to clean up the expression -- add parens to represents precedence.



Once, you do that, then it's easy....



Just follow the first branch for true, and the second branch for false.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ali bergen
Greenhorn

Joined: Apr 16, 2011
Posts: 5

Thanks a lot Henry for quick reply.

Still I dont understand, could please explain little more.

Thanks!
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

With the ternary operator a boolean expression is evaluated, if it is true the code before the colon is executed if it is false the code after the colon is evaluated.
(<boolean expression> ? "if true do this" : "if false do this").

It may be easier to see the answer to the nested example you posted if you draw it as a decision tree.

if the expression is true go down the left subtree, if false go down the right


Hope this helps,
Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8803
    
    5
This sample question is about 100x trickier than anything you'll get on the real exam!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Try it like this :
Single out each expression and replace it by the result, on paper. One line is enough.

true ? false ? "7" : false ? "6" : false ? true ? "5": >>>true ? "4" : "3"<<< : "2" :"1" ---> put 4 in place of expression.

true ? false ? "7" : false ? "6" : false ? >>>true ? "5": "4"<<< : "2" :"1" ---5

true ? false ? "7" : false ? "6" : >>>false ? "5" : "2"<<< :"1" ---etc


SCJP 6. Learning more now.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

@ Hunter McMillen : Hi, when you construct a tree, it is also important to tell that how to travel ?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Seetharaman Venkatasamy wrote:@ Hunter McMillen : Hi, when you construct a tree, it is also important to tell that how to travel ?


Yes, if you travel the wrong direction you will get the wrong answer. So if the condition you're evaluating is true go to the left child, if the condition is false, go to the right child.


Hunter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ternary Operator
 
Similar Threads
? ternary operator
? Operator
differences???
ternary operation
Potential Dumb Question.....