Java ?: operator

One of the least understood, and most underestimated Java language constructs is the ?: operator. Most people never even seen it in action. And those who did, never actually used it to do anything meaningful. Imagine something like this:

public generateFoo(int threshold)
{
    if(threshold > MIN_THRESHOLD)
       return new Foo();
    else
       return null;
}

In this snippet of code we have a function which generates a new Foo object if the passed argument is greater than some minimal threshold. If it is below threshold we return null. This is not an uncommon scenario… But with the ?: operator we could accomplish all of this on a single line.

public generateFoo(in threshold)
{
    return (threshold > MIN_THRESHOLD) ? new Foo() : null;
}

If you didn’t catch that let me show you something simpler:

int foo = bar ? a : b;

Java expects to a boolean or an expression evaluating to a boolean before the question mark. If that expression is true, then the whole statement evaluates to a. Else it evaluates to b.

It is elegant, produces a lean code and saves you one return statement. It is a good coding practice to have one return statement per method when possible. I think more Java programmers should embrace this little syntactic sugar. It saves you allot of typing when used appropriately.

Tags: , ,

Related Posts:

  • Arcane Java Operators
  • Matlab vs Java
  • Unsigned Primitives
  • Kubuntu + Java
  • March of Progress
  • Maintaining Sun Java Desktop System (R2)
  • Posting Twitter Updates using Java
  • The Joy of Java
  • What is the point of explicit typing again?
  • WTF Code moments…

  • 9 Responses to “Java ?: operator”

    1. Gravatar ellioman ICELAND Says: Reply to this comment

      Thanks for this!
      I needed a good explanation, and you gave me one )

      Cheers from Iceland.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.9 on Mac OS Mac OS X
    2. Gravatar Patrick CANADA Says: Reply to this comment

      public Playlist(Playlist playList){
      if (playList != null){
      for(int i = 0; i

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.6 on Linux Linux
    3. Gravatar Luke Maciak UNITED STATES Says: Reply to this comment

      Patrick - I think your comment got eaten. Remember, you can’t use the < and > unless you put them in the <pre> tag. Click the code button above the textbox. Observe:

      inside here I can use <> as normal

      Alternatively you can use the &lt; and &gt; HTML entities like I did above.

      Also, preview button does wonders for you. )

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.9 on Ubuntu Linux Ubuntu Linux
    4. Gravatar Patrick CANADA Says: Reply to this comment

      thx, I’ll repost.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.6 on Linux Linux
    5. Gravatar Patrick CANADA Says: Reply to this comment

      Well I used something like

      (boolean)? System.out.println(”Some text”):System.out.println(”Some other text”);

      Putting it in an if statement works no problem.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.6 on Linux Linux
    6. Gravatar Patrick CANADA Says: Reply to this comment

      But with the question mark operator, it did not work.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.6 on Linux Linux
    7. Gravatar Luke Maciak UNITED STATES Says: Reply to this comment

      Well, that is the one way you are not supposed to use the question mark statement. This statement is supposed to evaluate the boolean, and based on it’s value return the result of the statement to the left or the right of the colon.

      In other words, the two statements are required to return a value. Since println() is void, I believe this syntax will throw error. So you are right, in your example, the if statement is definitely the correct approach.

      To put it more simply, the question mark statement must evaluate to (or return) a value of some sort. It is not supposed to be used to perform an action.

      I hope that makes sense. )

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.9 on Ubuntu Linux Ubuntu Linux
    8. Gravatar Patrick CANADA Says: Reply to this comment

      Ya thanks, it make sense.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.6 on Linux Linux
    9. Gravatar test POLAND Says: Reply to this comment

      another advantage that it will not excute statement1:statement2 only the correct one

      Posted using Internet Explorer Internet Explorer 7.0 on Windows Windows XP

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]