return r == null ? -1 : r.height; // what is this?

Hello i am currently reading a book on AVLtree and i ran into a height method that returns the height. but i am not quite sure what does the single line mean. would anybody tell me how it works? thanks

/**

* Return the height of node t, or -1, if null.

*/

private int height( AvlNode<AnyType> t )

{

return t == null ? -1 : t.height;

}

[389 byte] By [mistykena] at [2007-11-26 21:45:28]
# 1

lol that's ternary logic

basically, on the left-hand-side of the ? is a boolean expression. if it evaluates to true, then the whole expression evaluates to the value between the ? and the :, otherwise it evaluates to the value on the far right. eg in your example, if r is null, then -1 is returned, otherwise, r.height is returned

a bit tricky to understand first time you see it, but very useful

georgemca at 2007-7-10 3:34:15 > top of Java-index,Java Essentials,New To Java...
# 2

So

return t == null ? -1 : t.height;

Is equivalent to

if (t==null)

return -1;

else

return t.height;

For simple logic like that, many (but not all) Java programmers prefer the operator over the if statement. It's especially handy embedded in larger expressions:

dude.setInstrument(lowIQ() ? drums : guitar);

DrLaszloJamfa at 2007-7-10 3:34:15 > top of Java-index,Java Essentials,New To Java...
# 3
oh i see. Thank you very much
mistykena at 2007-7-10 3:34:15 > top of Java-index,Java Essentials,New To Java...
# 4

> So

> > return t == null ? -1 : t.height;

>

> Is equivalent to

> > if (t==null)

>return -1;

>

>return t.height;

> de]

> For simple logic like that, many (but not all) Java

> programmers prefer the operator over the if

> statement. It's especially handy embedded in larger

> expressions:

> [code]

> dude.setInstrument(lowIQ() ? drums : guitar);

>

ah, another musician snob I see :-)

georgemca at 2007-7-10 3:34:15 > top of Java-index,Java Essentials,New To Java...
# 5

> For simple logic like that, many (but not all) Java

> programmers prefer the operator over the if

> statement. It's especially handy embedded in larger

> expressions:

> > dude.setInstrument(lowIQ() ? drums : guitar);

>

More specifically:

dude.setInstrument( lowIQ() ? (hasTalent() ? drums : (hasVoice()?vocals:bass) ) : guitar);

kevjavaa at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 6
I think you lot are listening to the wrong kind of bands...
DavidKNa at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 7

> > For simple logic like that, many (but not all)

> Java

> > programmers prefer the operator over the if

> > statement. It's especially handy embedded in

> larger

> > expressions:

> > > > dude.setInstrument(lowIQ() ? drums : guitar);

> >

>

> More specifically:

> > dude.setInstrument( lowIQ() ? (hasTalent() ? drums :

> (hasVoice()?vocals:bass) ) : guitar);

>

or maybe

dude.setInstument( hasBasement() || girlfriend.isUnderstanding() ? drums : guitar )

georgemca at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 8
> I think you lot are listening to the wrong kind of> bands...I strongly disagree
georgemca at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 9
> or maybe> > dude.setInstument( hasBasement() || girlfriend.isUnderstanding() ? drums : guitar ) throws GirlfriendBetterNotBeFoundException at com.theband.Wife.isUnderstanding(Wife.java:36)...
kevjavaa at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 10

dude.setInstrument( lowIQ() ? (hasTalent() ? drums : (hasVoice()?vocals:bass) ) : guitar);

well i am fairly new at this(being the one who asked question) but i decide to give it a try.....

so the statement is basically saying those who has lowIQ no talent no voice will be bass guitarlist?

but i think bass guitar rockz.......just my opinion....

mistykena at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 11
> dude.setInstrument(lowIQ() ? drums : guitar);Thems fightin' words.
KelVarnsona at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 12
BEST THREAD EVER
TuringPesta at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 13
> > dude.setInstrument(lowIQ() ? drums :> guitar);> Thems fightin' words.doesn't matter. we can just write "PTO" on both sides of a bit of paper, and make a leisurely escape while the drummer descends into a black hole of eternal fascination and drool
georgemca at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 14

> doesn't matter. we can just write "PTO" on both sides of a bit of paper,

> and make a leisurely escape while the drummer descends into a

> black hole of eternal fascination and drool

But eventually the drummer will figure it out, and then proceed to pound on the guitar player in 4 different time signatures at the same time.

KelVarnsona at 2007-7-10 3:34:16 > top of Java-index,Java Essentials,New To Java...
# 15
presumably the long pause indicates all the resident drummers running off to look up what "PTO" means :-)
georgemca at 2007-7-21 18:25:55 > top of Java-index,Java Essentials,New To Java...
# 16

> > doesn't matter. we can just write "PTO" on both

> sides of a bit of paper,

> > and make a leisurely escape while the drummer

> descends into a

> > black hole of eternal fascination and drool

>

> But eventually the drummer will figure it out, and

> then proceed to pound on the guitar player in 4

> different time signatures at the same time.

none of which will be relevant to the song in question

georgemca at 2007-7-21 18:25:55 > top of Java-index,Java Essentials,New To Java...
# 17

> presumably the long pause indicates all the resident

> drummers running off to look up what "PTO" means :-)

LMAO - I actually tried in myself, you see. I wrote it down just like you said, and then was astonished to see just how many sides a piece of paper can actually have. It's like magic!

14...

15...

16...

17...

KelVarnsona at 2007-7-21 18:25:55 > top of Java-index,Java Essentials,New To Java...
# 18
> none of which will be relevant to the song in questionThe solo to "Wipeout" is suitable for all occasions, including the one in question.
DrLaszloJamfa at 2007-7-21 18:25:55 > top of Java-index,Java Essentials,New To Java...
# 19

> > presumably the long pause indicates all the

> resident

> > drummers running off to look up what "PTO" means

> :-)

>

> LMAO - I actually tried in myself, you see. I wrote

> it down just like you said, and then was astonished

> to see just how many sides a piece of paper can

> actually have. It's like magic!

>

> 14...

> 15...

> 16...

> 17...

heh heh see, I knew drummers weren't thick!

I never used to believe drummers were stupid, until my former bass-player - of reasonable intellect - switched to drums, and subsequently dropped some 30 IQ points. no, really. very odd

georgemca at 2007-7-21 18:25:55 > top of Java-index,Java Essentials,New To Java...
# 20

> The solo to "Wipeout" is suitable for all occasions, including the one in question.

Altogether now...Der-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner

Der-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner

Der-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner

Der-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner-ner

KelVarnsona at 2007-7-21 18:25:55 > top of Java-index,Java Essentials,New To Java...
# 21
I forgot the solo:Boogadda-boogadda-boogadda-boogadda-boogadda-boogadda-boogadda-boogadda-boogaddaAhh forget it - my arms are tired
KelVarnsona at 2007-7-21 18:25:55 > top of Java-index,Java Essentials,New To Java...