Tertiary Conditional: what does this evaluate to ("docRoot == null ? this.root : doc root")?

Discussion in 'Java' started by Rick Osborn, Feb 3, 2004.

  1. Rick Osborn

    Rick Osborn Guest

    I couldn't find anything in any of my text's.
    Just the name.
    Rick Osborn, Feb 3, 2004
    #1
    1. Advertising

  2. Rick Osborn

    Andrew Hobbs Guest

    You should find it in one of your texts as the ternary operator (not
    tertiary).

    If not (I would be surprised if it is omitted from a basic java textbook)
    look at

    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html

    Andrew


    --
    ********************************************************
    Andrew Hobbs PhD

    MetaSense Pty Ltd - www.metasense.com.au
    12 Ashover Grove
    Carine W.A.
    Australia 6020

    61 8 9246 2026
    metasens AntiSpam @iinet dot net dot au


    *********************************************************

    "Rick Osborn" <> wrote in message
    news:...
    > I couldn't find anything in any of my text's.
    > Just the name.
    Andrew Hobbs, Feb 3, 2004
    #2
    1. Advertising

  3. The ternary operator is shorthand for:

    if(docRoot == null)
    {
    this.root;
    }
    else
    {
    doc root;
    }

    Your code doesn't look legit, but that's not your question.

    doug

    "Rick Osborn" <> wrote in message
    news:...
    > I couldn't find anything in any of my text's.
    > Just the name.
    Doug Schwartz, Feb 3, 2004
    #3
  4. Rick Osborn

    hiwa Guest

    (Rick Osborn) wrote in message news:<>...
    > I couldn't find anything in any of my text's.
    > Just the name.

    someThing = docRoot == null ? this.root : doc root;
    //if docRoot is null, then assign this.root to someThing
    //if docRoot is not null, then assign doc.root to someThing
    //'doc root' is a wrong Java syntax
    hiwa, Feb 3, 2004
    #4
  5. Doug Schwartz <> scribbled the following:
    > The ternary operator is shorthand for:


    > if(docRoot == null)
    > {
    > this.root;
    > }
    > else
    > {
    > doc root;
    > }


    > Your code doesn't look legit, but that's not your question.


    This gives the impression that this code:

    (docRoot == null ? this.root : docRoot).someMethod();

    is shorthand for:

    if (docRoot == null) {
    this.root;
    }
    else {
    docRoot;
    }.someMethod();

    which is very much invalid Java syntax. It is really shorthand for:

    if (docRoot == null) {
    this.root.someMethod();
    }
    else {
    docRoot.someMethod();
    }

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Immanuel Kant but Genghis Khan."
    - The Official Graffitist's Handbook
    Joona I Palaste, Feb 3, 2004
    #5
  6. Re: Tertiary Conditional: what does this evaluate to ("docRoot ==

    Joona I Palaste wrote:

    > Doug Schwartz <> scribbled the following:
    >
    >>The ternary operator is shorthand for:

    >
    >
    >>if(docRoot == null)
    >>{
    >> this.root;
    >>}
    >>else
    >>{
    >> doc root;
    >>}

    >
    >
    >>Your code doesn't look legit, but that's not your question.

    >
    >
    > This gives the impression that this code:
    >
    > (docRoot == null ? this.root : docRoot).someMethod();
    >
    > is shorthand for:
    >
    > if (docRoot == null) {
    > this.root;
    > }
    > else {
    > docRoot;
    > }.someMethod();
    >
    > which is very much invalid Java syntax. It is really shorthand for:
    >
    > if (docRoot == null) {
    > this.root.someMethod();
    > }
    > else {
    > docRoot.someMethod();
    > }
    >


    Keep in mind that the ?: ternary is an expression, not flow control.
    It's used to choose from two values. You can't use it if the values are
    void (ie a void method).
    David Zimmerman, Feb 3, 2004
    #6
  7. Rick Osborn

    Rick Osborn Guest

    It's amazing. I only have exam guides left and none had it.
    Only examples using it.

    They also have "+=" but none explain in detail. I assume it's
    shorthand for concatenation/re-assignment. But none says for sure.
    Am I right?





    "Andrew Hobbs" <> wrote in message news:<401f2c4c$0$1745$>...
    > You should find it in one of your texts as the ternary operator (not
    > tertiary).
    >
    > If not (I would be surprised if it is omitted from a basic java textbook)
    > look at
    >
    > http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html
    >
    > Andrew
    >
    >
    > --
    > ********************************************************
    > Andrew Hobbs PhD
    >
    > MetaSense Pty Ltd - www.metasense.com.au
    > 12 Ashover Grove
    > Carine W.A.
    > Australia 6020
    >
    > 61 8 9246 2026
    > metasens AntiSpam @iinet dot net dot au
    >
    >
    > *********************************************************
    >
    > "Rick Osborn" <> wrote in message
    > news:...
    > > I couldn't find anything in any of my text's.
    > > Just the name.
    Rick Osborn, Feb 4, 2004
    #7
  8. Rick Osborn

    Tony Morris Guest

    "Rick Osborn" <> wrote in message
    news:...
    > I couldn't find anything in any of my text's.
    > Just the name.


    It's a String literal:
    String s = ("docRoot == null ? this.root : doc root")


    If you meant:
    docRoot == null ? this.root : doc root

    The ternary (not tertiary) operator is evaluated as follows:
    (condition ? expression1 : expression2)

    if(condition)
    {
    evaluate as expression1
    }
    else
    {
    evaluate as expression2
    }

    Therefore, your corrected statement is evaluated as follows:
    if(docRoot == null)
    {
    evaluate as this.root
    }
    else
    {
    // I suspect a compile-time error will occur because of this
    evaluate as doc root
    }


    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
    Tony Morris, Feb 4, 2004
    #8
  9. Re: Tertiary Conditional: what does this evaluate to ("docRoot ==

    Rick Osborn wrote:
    > It's amazing. I only have exam guides left and none had it.
    > Only examples using it.


    What a lame excuse. Let's face it. There is a boatload of beginner Java
    information out there on the net. You could e.g. start with the
    tutorials on Sun's web site. Or the language specification (on Sun's web
    site, too). Or Bruce Eckel's "Thinking in Java", also for free on the net.

    You could also start to ask your beginner's questions in
    comp.lang.java.help - where they are on-topic.

    > They also have "+=" but none explain in detail. I assume it's
    > shorthand for concatenation/re-assignment. But none says for sure.
    > Am I right?


    See above.

    And what about reading the answers you already got? Hint: Andrew's
    answer pointed you to a web site explaining that operator, too.

    Get your lazy ass up, start reading and stop whining that your can't
    find things in your text book.

    /Thomas
    Thomas Weidenfeller, Feb 4, 2004
    #9
  10. Rick Osborn

    Tony Morris Guest

    > Get your lazy ass up, start reading and stop whining that your can't
    > find things in your text book.


    Here here !
    Blunt, completely accurate and I'd even speculate that it's the general
    opinion of most.
    I like it.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
    Tony Morris, Feb 4, 2004
    #10
  11. Rick Osborn

    Jon A. Cruz Guest

    Re: Tertiary Conditional: what does this evaluate to ("docRoot ==

    Rick Osborn wrote:
    > It's amazing. I only have exam guides left and none had it.
    > Only examples using it.
    >
    > They also have "+=" but none explain in detail. I assume it's
    > shorthand for concatenation/re-assignment. But none says for sure.
    > Am I right?



    Yes.

    But your text *really* should have covered those.

    Operators:
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

    Handy summary:
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html
    Jon A. Cruz, Feb 8, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    3
    Views:
    387
  2. Harry
    Replies:
    8
    Views:
    851
    Danno
    Jan 29, 2007
  3. abcd

    Tertiary Operation

    abcd, Oct 17, 2006, in forum: Python
    Replies:
    10
    Views:
    1,696
    Fredrik Lundh
    Oct 17, 2006
  4. Taran
    Replies:
    2
    Views:
    320
    Frederick Gotham
    Jul 25, 2006
  5. Sideswipe
    Replies:
    4
    Views:
    431
Loading...

Share This Page