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]

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
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);
oh i see. Thank you very much
> 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 :-)
> 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);
I think you lot are listening to the wrong kind of bands...
> > 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 )
> I think you lot are listening to the wrong kind of> bands...I strongly disagree
> or maybe> > dude.setInstument( hasBasement() || girlfriend.isUnderstanding() ? drums : guitar ) throws GirlfriendBetterNotBeFoundException at com.theband.Wife.isUnderstanding(Wife.java:36)...
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....
> dude.setInstrument(lowIQ() ? drums : guitar);Thems fightin' words.
> > 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
> 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.
presumably the long pause indicates all the resident drummers running off to look up what "PTO" means :-)
> > 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
> 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...
> none of which will be relevant to the song in questionThe solo to "Wipeout" is suitable for all occasions, including the one in question.
> > 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
> 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
I forgot the solo:Boogadda-boogadda-boogadda-boogadda-boogadda-boogadda-boogadda-boogadda-boogaddaAhh forget it - my arms are tired