Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

" : " and " ? " in java

Options
  • 16-02-2006 1:19pm
    #1
    Registered Users Posts: 3,299 ✭✭✭


    private static int max( int lhs, int rhs )
    {
    return lhs > rhs ? lhs : rhs;
    }


    can some expalin what the " ? " and " : " mean in the above code please


Comments

  • Registered Users Posts: 919 ✭✭✭timeout


    its an if else situation.
    if (lhs > rhs){
                return lhs;
              } else{
                return rhs;
              }
    

    its just hander for simple comparison expressions without the need of all the extra text and brackets associated with the if else. So the ? represents what to do if the expression is true and : if its false.

    Timeout


  • Registered Users Posts: 3,299 ✭✭✭irishguy


    timeout wrote:
    its an if else situation.
    if (lhs > rhs){
                return lhs;
              } else{
                return rhs;
              }
    

    its just hander for simple comparison expressions without the need of all the extra text and brackets associated with the if else. So the ? represents what to do if the expression is true and : if its false.

    Timeout
    Cool thanks, never seen it before


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    It's called the Ternary operator, it originates from C.

    http://www.devdaily.com/java/edu/pj/pj010018/


  • Registered Users Posts: 1,821 ✭✭✭Skud


    In performance tests on a Sun Solaris workstation, the ternary operator was four times faster than the Math.min() method when comparing float variables, and twice as fast when comparing integer variables. In similar cases in your code, I recommend testing to determine if performance improvements can be achieved. As the old saying goes, your performance may vary.
    ... You learn something new everyday. That obviously doesnt correspond to unix or windows systems exactly... So it's better to use this?


  • Registered Users Posts: 32,136 ✭✭✭✭is_that_so


    TBH , don't use it with anything more complex than your example. It leads to very terse code that is extremely hard to maintain and you may not even remember what you were doing in six months time. :rolleyes: If you do have to use it do so sparingly. It's a tradeoff between efficiency and maintainability.
    Nothing wrong with if .....else :)


  • Advertisement
  • Registered Users Posts: 7,498 ✭✭✭BrokenArrows


    cool never saw that before.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    is_that_so wrote:
    TBH , don't use it with anything more complex than your example.

    I agree, although I did get something like this in the exam.

    answer = lhs++ > rhs-- ? lhs++ : rhs++;

    You had to list the values, but in certain cases the numbers won't increment/decrement.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    answer = lhs++ > rhs-- ? lhs++ : rhs++;

    Converts to...
    if(lhs++ > rhs--)
    {
        lhs++;
    }
    else
    {
        rhs++;
    }
    
    Doesn't it? Also, i assume that if it evaluates true... lhs will end up being 2 bigger, and rhs will be one smaller.

    If it evaluates false, lhs ends up one bigger, and rhs ends up with its initial value? At least their all post increments and not a mixture of pre- and post-increments, otherwise it'd be even more confusing :P


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Yeah that's right, and answer would be assigned inside there, ending up with the relevant value before the increment.

    Interesting question, what exam did you get that in Hobbes?


  • Registered Users Posts: 16,766 ✭✭✭✭Nalz


    irishguy wrote:
    Cool thanks, never seen it before

    I honestly think C should be taught (in programming courses) before java. it would make life a lot better for future porgammers...but that argument is for another day i suppose


  • Advertisement
  • Registered Users Posts: 32,136 ✭✭✭✭is_that_so


    Trilla wrote:
    I honestly think C should be taught (in programming courses) before java. it would make life a lot better for future porgammers...but that argument is for another day i suppose
    Or put you off.:p


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    satchmo wrote:
    Interesting question, what exam did you get that in Hobbes?

    SCJP1.4

    a lot of the questions are trick questions.


  • Registered Users Posts: 919 ✭✭✭timeout


    you could get smacked down my sun for saying that having signed their disclaimer in all :D Doubt they'll follow through since its 1.5 now


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    is_that_so wrote:
    Or put you off.:p

    Anyone put off by C shouldn't be a programmer anyway.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    timeout wrote:
    you could get smacked down my sun for saying that having signed their disclaimer in all :D Doubt they'll follow through since its 1.5 now

    hardly. it is not an exact copy of the question, because tbh I can't remember the exact question but you do get ones along that lines.

    You would learn more from jchq.net tbh.


  • Registered Users Posts: 16,766 ✭✭✭✭Nalz


    rsynnott wrote:
    Anyone put off by C shouldn't be a programmer anyway.

    very very true


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    You're not a true programmer til you've malloced and realloced!

    Still... i got through 18 months of C programming in Electronic Engineering without having to use malloc. I used it for the first time last week because i felt i wasn't a real programmer yet. Now i am :p


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    You're not a true programmer til you've malloced and realloced!

    Still... i got through 18 months of C programming in Electronic Engineering without having to use malloc. I used it for the first time last week because i felt i wasn't a real programmer yet. Now i am :p

    Next step: LISP!


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Lisp or scheme (currently hacking around in scheme in uni)

    Actually with C, you can do some incredibly fun hacks, because of the way it evaluates true/false... i.e. false is 0, anything else is true.

    So to test for odd/even numbers
    (i % 2)? something : something else.

    One hack I was particularly proud of (because there's one lecturer we have and he seems to promote the use of hackish/obscure code while learning C++) involved something like
    cout << ((++on % 2) ? "Light is on" : "Light is off");
    

    So hacky! So painfully, painfully hacky! However, being a masochist, I found it strangely fun! :D

    aoife


  • Registered Users Posts: 4,003 ✭✭✭rsynnott




  • Advertisement
  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Yeah, I'm not that much of a masochist... yet.

    I just find that I've written it to be just enough of a pain to rewrite if I ever need to.

    But yeah, I just like the fun that can be had with c's interesting syntax... and ternary operators are definately one of 'em. :)


Advertisement