| Author |
System.out.print(s + (b ? "T" : "F"));
|
Jerry Bustamente
Ranch Hand
Joined: May 24, 2004
Posts: 90
|
|
I understand the flow of the following code but do not understand how the syntax System.out.print(s + (b ? "T" : "F")); resolves to a proper conditional expression especially since the (b ? "T" : "F") part of the expression is never assigned to a variable but appended to one - s. package testPackage; public class EBH204 { static boolean m1(String s, boolean b) { System.out.println(" "); System.out.println("just entered m1"); System.out.println("b before conditional operation " + b); System.out.println("s before conditional operation " + s); System.out.println(" "); System.out.print(s + (b ? "T" : "F")); System.out.println(" "); System.out.println("b after conditional operation " + b); System.out.println("s after conditional operation = " + s); return b; } public static void main(String[] args) { System.out.println("just entered main"); m1("A", m1("B",false) || m1 ("C", true) || m1("D",false)); //System.out.println(" A and B " + (m1("A", m1("B",false)))); System.out.println(" A and B and C " + (m1("A", m1("B",false) || m1 ("C", true)))); System.out.println("just executed m .... in main"); } } Output: just entered main just entered m1 b before conditional operation false s before conditional operation B BF b after conditional operation false s after conditional operation = B just entered m1 b before conditional operation true s before conditional operation C CT b after conditional operation true s after conditional operation = C just entered m1 b before conditional operation true s before conditional operation A AT b after conditional operation true s after conditional operation = A just entered m1 b before conditional operation false s before conditional operation B BF b after conditional operation false s after conditional operation = B just entered m1 b before conditional operation true s before conditional operation C CT b after conditional operation true s after conditional operation = C just entered m1 b before conditional operation true s before conditional operation A AT b after conditional operation true s after conditional operation = A A and B and C true just executed m .... in main Can someone advise me? Thanks, JerryB
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
The tertiary operator ?: returns a value, just like a function returns a value. Just as you can do this: or this: you can do this: or this I hope that helps, Corey
|
SCJP Tipline, etc.
|
 |
Jerry Bustamente
Ranch Hand
Joined: May 24, 2004
Posts: 90
|
|
Thanks Corey. What threw me is that on page 176 at the top of K&B the code structure reads: someVariable = (boolean expression) ? value to assign if true: value to assign if false From reading that I believed that the results of the ?: would always need to be assigned to a variable ( because of the assignment = ) whereas it can be used to compute values, assign to a variable, or control the flow of execution. Thanks alot. JerryB
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
As an add-on to Corey's answer, you can consider the expression b?"A":"B" as an actual method:
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Jerry Bustamente
Ranch Hand
Joined: May 24, 2004
Posts: 90
|
|
Thanks alot Barry. That also helps. :-) JerryB
|
 |
 |
|
|
subject: System.out.print(s + (b ? "T" : "F"));
|
|
|