1 2 3 Previous Next 70 Replies Latest reply on Mar 10, 2010 6:33 PM by 796440

    Ternary operator

    843807
      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.
        • 1. Re: Ternary operator
          walterln
          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);
          • 2. Re: Ternary operator
            843807
            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.
            • 3. Re: Ternary operator
              843807
              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?
              • 4. Re: Ternary operator
                800330
                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());
                • 5. Re: Ternary operator
                  darrylburke
                  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
                  • 6. Re: Ternary operator
                    843807
                    >
                    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.
                    • 7. Re: Ternary operator
                      843807
                      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.
                      • 8. Re: Ternary operator
                        843807
                        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.
                        • 9. Re: Ternary operator
                          796262
                          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?
                          • 10. Re: Ternary operator
                            796262
                            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.
                            • 11. Re: Ternary operator
                              walterln
                              atch7 wrote:
                              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.
                              No, you said that the compiler told you it wasn't a statement and the compiler is always right (sorry :)).
                              atch7 wrote:
                              So following this logic I can't say:

                              int == 0 ? setVisible(true) : setVisible(false);
                              Again why would you want to say that when setVisible(i == 0) is much shorter and clearer. Note I'm ignoring the completely invalid syntax int == 0.
                              int a = i == 0 ? setSelectable(1) : setSelectable(0);
                              Again there is a shorter form: setSelectable(i == 0 ? 1 : 0); It even uses the ternary operator we all love correctly and your dummy function isn't needed.

                              But you might want to look at Groovy (or similar language) which allows this kind of syntax. Java just doesn't.
                              • 12. Re: Ternary operator
                                843807
                                >
                                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.
                                It happens that I have c++ background and in c++ this kind of syntax works. There is no need to get agitated. I am not saying to anyone to change programming language. The only thing I'm saying is that in java, very often things works in illogical way which frustrates me. I think I have right to get frustrated if something doesn't work in a logical way. And don't give me any suggestions.

                                Edited by: atch7 on Jan 28, 2010 6:37 AM

                                Edited by: atch7 on Jan 28, 2010 6:47 AM
                                • 13. Re: Ternary operator
                                  843807
                                  kevinaworkman wrote:
                                  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.
                                  Why this:
                                  1+1
                                  doesn't make any sense to you?
                                  • 14. Re: Ternary operator
                                    843807
                                    kevinaworkman wrote:
                                    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?
                                    If you can't grasp the simple concept behind the ternary operator's behavior, maybe it's time to consider a career change?
                                    I just want to point out that in my original post I 've used this operator correctly, but because java doesn't behave logically (sometimes), I was getting an error.
                                    1 2 3 Previous Next