What does this code mean?

System.out.println((age < 4)?"Kid":(age > 18)?"Adult":"Not yet supported");
[163 byte] By [Jamwaa] at [2007-11-27 8:00:28]
# 1

http://en.wikipedia.org/wiki/%3F:

if (age < 4) {

System.out.println("Kid");

} else if (age > 18) {

System.out.println("Adult");

} else {

System.out.println("Not yet supported");

}

~

yawmarka at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 2

its a simple conditional statement that can also be written as follows

if(age < 4)

System.out.println("kid");

else if(age>18)

System.out.println("Adult");

else

System.out.println("Not Yet Supported");

This format is in every basic programming language like C, C++ and Java and is called the conditional if statement.

@@CKM@@a at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 3
> This format is in every basic programming language> like C, C++ and Java and is called the conditional if> statement.By implication, then, there exists an undonditional "if" statement :-)The code in question makes use of the ternary operator
georgemca at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 4

> > System.out.println((age < 4)?"Kid":(age >

> 18)?"Adult":"Not yet supported");

>

Agree w/ the statements above. I have to say though that this line isn't quickly readable and in my opinion would make debugging somewhat more difficult. It's starting to smell like C here.

petes1234a at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 5

> > > > System.out.println((age < 4)?"Kid":(age >

> > 18)?"Adult":"Not yet supported");

> >

>

> Agree w/ the statements above. I have to say though

> that this line isn't quickly readable and in my

> opinion would make debugging somewhat more difficult.

> It's starting to smell like C here.

Disagree with that. Once you know about the ternary operator, it's much easier to read than the expanded conditional logic, IMHO. Of course, this relies on one knowing about the ternary operator, but then, a working knowledge of the language should be a fair assumption to make in any case.

georgemca at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 6
> > Disagree with that. Once you know about the ternary> operator, it's much easier to read than the expanded> conditional logic, I disagree.
cotton.ma at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 7
> I disagree.I agree.
hunter9000a at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 8

The java terniary short circuits.. for example

applet.size = (args.length == 0 ? 5 : Integer.parseInt(args[0]));

From memory, this would fail in C, because both possibilities are evaluated and then the correct one is chosen.

... and here's one I used yesterday ...

for (int i=0; i<TIMES; i++) {

double x = random.nextDouble(low, high);

System.err.format("%-11.2f%s", x, (i+1%4==0?"\n":"\t") );

if(x><low || x>high) throw new AssertionError("x="+x+" is not between "+low+" and "+high);

if(x==low||x==high) {

System.err.println("Bingo x="+x+"!"); break;

}

}

The equivalent "long hand" would make the debug code bigger than the real code.

corlettka at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 9

> >

> > Disagree with that. Once you know about the

> ternary

> > operator, it's much easier to read than the

> expanded

> > conditional logic,

>

> I disagree.

You're wrong ;-)

Seriously, though, it's often better to be verbose and aim for clarity, sure, but sometimes a sprinkling of conditional logic is so trivial I'd prefer to confine it to a single line using the ternary operator. Mentally, when reading code, I'll find myself narrating a set of if statements, along the lines of "if that evaluates to true, do that. If it evaluates to false, do that." or similar, whereas ternary logic seems to just pop in and out of my train of thought in an instant: "it's either that or that" and we're done.

I have a natural aversion to "if" statements anyway. Been bitten by some nasty ones :-)

georgemca at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 10
Single ternary operator > if/else > nested ternary operators. Once you get more than one ? : a single line it makes it harder to read that if/elses.
hunter9000a at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 11
> Single ternary operator > if/else > nested ternary> operators. > > Once you get more than one ? : a single line it makes> it harder to read that if/elses.I agreeStill partial to the old ternary operator, though. Feels
georgemca at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 12
i both agree and disagree at the same time, hense, you are both wrong (and right)
mkoryaka at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 13
I agree, you used to have to do that with iif() in excel... they could get seriously nasty, especially populated with such exquisitely meaningful variables names as A:1 through ZC:1
corlettka at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 14
I've got nothing against a ternary operator, but nesting them makes me sit back and go "huh"? It's like I hit a speed bump when I'm trying to read code. I hate speed bumps.
petes1234a at 2007-7-12 19:42:26 > top of Java-index,Java Essentials,New To Java...
# 15
Yep, ternaries should definately stop a three.condition ? truevalue : falsevalueafter that it gets nasty really quickly.
corlettka at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...
# 16
I'm confused. Who's agreeing with whom?
georgemca at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...
# 17
> I'm confused. Who's agreeing with whom?Everyone and no one simultaneously.
hunter9000a at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...
# 18
Let's all hold hands and sing "kumbaya"
petes1234a at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...
# 19
> I'm confused. Who's agreeing with whom?I believe we are all agreed that you smell of elderberries and that I look good and thus deserve more money. Except for myokrak who thinks properly using PreparedStatements would render the whole discussion moot.
cotton.ma at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...
# 20
I can't possibly smell of elderberries. Any fruity smell at the moment is massively overwhelmed by the unmistakable stench of someone who's just got back from a heavy metal festival
georgemca at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...
# 21
> I can't possibly smell of elderberries. Any fruity> smell at the moment is massively overwhelmed by the> unmistakable stench of someone who's just got back> from a heavy metal festivalahh, the sweet smell of wee :-)
JonlWrighta at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...
# 22

> > I can't possibly smell of elderberries. Any fruity

> > smell at the moment is massively overwhelmed by

> the

> > unmistakable stench of someone who's just got back

> > from a heavy metal festival

>

> ahh, the sweet smell of wee :-)

Yeh, you can probably detect that under the whiff of highly-dubious sweat

georgemca at 2007-7-21 22:25:50 > top of Java-index,Java Essentials,New To Java...