This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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

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

 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks alot Barry. That also helps.

:-)

JerryB
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic