• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

System.out.print(s + (b ? "T" : "F"));

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an add-on to Corey's answer, you can consider the expression b?"A":"B" as an actual method:
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Barry. That also helps.

:-)

JerryB
reply
    Bookmark Topic Watch Topic
  • New Topic