d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Ternary Operator Question
Add Reply New Topic New Poll
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Sep 5 2017 04:09am
Code
isCar = true;
boolean wasCar = isCar ? true : false;
if(wasCar)
System.out.println("wasCar is true");


Couldn't i just do:

Code
isCar = true;
boolean isCar ? true : false;
if(isCar)
System.out.println("wasCar is true");


This post was edited by ferf on Sep 5 2017 04:12am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 5 2017 07:50pm
the syntax is:

booleanExpression ? value : anotherValue

and it evaluates to the value or anotherValue

look at your first example
boolean wasCar = isCar ? true : false;

after evaluation:
boolean wasCar = true;

now try your second example
boolean isCar ? true : false;

after evaluation:
boolean true;

does that look valid to you?
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Sep 6 2017 11:46am
Quote (carteblanche @ Sep 5 2017 09:50pm)
the syntax is:

booleanExpression ? value : anotherValue

and it evaluates to the value or anotherValue

look at your first example
boolean wasCar = isCar ? true : false;

after evaluation:
boolean wasCar = true;

now try your second example
boolean isCar ? true : false;

after evaluation:
boolean true;

does that look valid to you?


Based on what you said, yes it's valid.... i'd think...
thanks

This post was edited by ferf on Sep 6 2017 11:46am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 6 2017 05:56pm
Quote (ferf @ Sep 6 2017 01:46pm)
Based on what you said, yes it's valid.... i'd think...
thanks


how do you figure?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll