Forum Stats

  • 3,770,911 Users
  • 2,253,180 Discussions
  • 7,875,653 Comments

Discussions

Ternary operator

843807
843807 Member Posts: 46,582
edited Mar 10, 2010 1:33PM in Swing
Hi,
Guys, what I'm doing wrong in here? I'm getting an error that this isn't a statement.
i == 0 ? tmp.setSelected(true) : tmp.setSelected(false);
tmp is a JRadioButton;

Thank you for any help.
«134567

Comments

  • walterln
    walterln Member Posts: 2,302
    The ternary operator is an expression not a statement:
    // OK
    int i = true ? 42 : 0;
    
    // not OK
    true ? 42 : 0;
    
    // what you actually want:
    tmp.setSelected(i == 0);
  • 843807
    843807 Member Posts: 46,582
    I didn't say that ternary operator is a statement. What I'm saying though is that you should be able to place expressions into this operator.
  • 843807
    843807 Member Posts: 46,582
    This operator suppose to be a shorthand for if then else so why I can write something like:
    if (i == 0)
    {
    tmp.setVisible(true);
    }
    else
    {
    tmp.setVisible(false);
    }
    but not as I wrote in my original post?
  • 800330
    800330 Member Posts: 355
    It is not just short hand for the if statement as you say, it is an if-like construction but as an expression as Walter says. An expression is (loosely said) something that appears on the right hand side of an assignment.

    You would not expect the following to be working would you?
       public void testExpression() {
          1+1;
       }
    Some people use the ternary like this
      bean.setField(input==null?"default value":input.toString());
  • darrylburke
    darrylburke Member Posts: 18,007
    atch7 wrote:
    This operator suppose to be a shorthand ...
    If this is a real-world example, the 'shorthand' you need is
    tmp.setSelected(i == 0);
    db
  • 843807
    843807 Member Posts: 46,582
    >
    You would not expect the following to be working would you?
    public void testExpression() {
    1+1;
    }
    Well as a matter of fact I would indeed expect that to work. Why wouldn't I ? If it is correct java syntax, and 1+1 is I would expect correct syntax to work.
  • 843807
    843807 Member Posts: 46,582
    It's another of java's illogicalities. In my code mentioned in original post if I have to use a ternary operator to do what I want it to do:
    1. I have to make sure that I'm using methods which return some value different than void and
    2. I have to assign it to some completely unnecessary variable.

    So following this logic I can't say:

    int == 0 ? setVisible(true) : setVisible(false);

    but providing that I have method definition:

    int setSelectable(int a)
    {
    //do something
    //and just for sake of making this to work I have to have return statement
    return a;
    }

    having that now I can write

    int a = i == 0 ? setSelectable(1) : setSelectable(0);

    Pathetic, illogical and completely java like behavior.
    Guys, I'm telling you, every time when I have to do something in java I just get irritated and frustrated. Something what in other languages is simple and logical and work as you would imagine it to work in java is very often somewhat different, illogical and annoying.
  • 843807
    843807 Member Posts: 46,582
    Another thing is that it is an operator just like '+' or '-' and as long as it have correct operands on each of its side it should work without making any problems.
  • 796262
    796262 Member Posts: 2,678
    atch7 wrote:
    Pathetic, illogical and completely java like behavior.
    So, let me get this straight: you don't know how to use a ternary operator, so instead of learning and correcting yourself, you conclude that java must be in the wrong? You asked a question and received a correct answer (although, any programmer worth a nickel should have been able to interpret the compiler message you got). But instead of manning up and learning from your mistakes, you're just going to complain that you were wrong? Yeah, it's much better to go off ignorant assumptions instead of learning and using a clearly defined behavior. You're going to make it pretty far as a programmer...
    Guys, I'm telling you, every time when I have to do something in java I just get irritated and frustrated. Something what in other languages is simple and logical and work as you would imagine it to work in java is very often somewhat different, illogical and annoying.
    Oh boy, I'm soo glad you pointed that out. Thankfully now, the millions of people who successfully use java will come to their senses and abandon this awful language. What would you suggest we use instead, oh master of logic?

    If you can't grasp the simple concept behind the ternary operator's behavior, maybe it's time to consider a career change?
  • 796262
    796262 Member Posts: 2,678
    atch7 wrote:
    Another thing is that it is an operator just like '+' or '-' and as long as it have correct operands on each of its side it should work without making any problems.
    What? Why? Says who? This doesn't make any sense.

    Your gross misunderstanding of the basics suggests that you should refamiliarize yourself with the simple concepts. I suggest starting here . I also suggest dropping the attitude before you ask any more questions.
This discussion has been closed.