This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why to use conditional operator " ? " ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess conditional operator "?" is similar to " if else block " ?
  • if yes , then why should we use "?" instead of " if else " which is much more simple to read & understand ?
  • and also many books like effective java , jls and many other uses this operator "?" instead of simple if else statement , why so ? whats so special about "?" operator


  • note : i know that it can be used to reduce the lines of code , so please give some more reasons why i should use this "?" operator

  •  
    David Phluphy
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Cutting this straight from the wiki:

    In C and C-like languages conditional expressions take the form of a ternary operator called the conditional expression operator, ?:, which follows this template:

    This means that conditions can be inlined into expressions, unlike with if statements, as shown here using C syntax:

    To accomplish the same as the second (correct) line above, using a standard if/else statement, this would take more than one line of code (under standard layout conventions):


    Source: http://en.wikipedia.org/wiki/Conditional_(programming)
     
    John Jai
    Rancher
    Posts: 1776
     
    Martin Vajsar
    Sheriff
    Posts: 3752
    62
    Chrome Netbeans IDE Oracle
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'd say this is very subjective topic. From my point of view, the ternary operator does not seem at all to be harder to read than an if-else construct. I use them a lot. I sometimes use nested ternaries too, but this is already clearly quite controversial.

    Two points in favor of ternaries:

    1) Indispensable in declaring final variables:
        final int size = large ? 1000 : 100;
    If-else alternative is clearly less readable, since it involves either additional temporary variable, or a method.

    2) Reducing amount of lines: the if-else construct takes four to five lines, where the ternary will usually be one or two. I generally work with display where I can see 40 to 50 lines of code at once, but when on notebook, it may be reduced to 20 lines or so. I find seeing more of the surrounding code beneficial in both cases.

    Edit: I see the original poster has already mentioned my second point. Yet I consider it very important reason to use ternary operator in itself - based of course on my belief that ternary operator is at least as readable as the if-else replacement.

    However, I do avoid ternary expressions with side effects (eg. calling methods that change state of an object) like a plague. Something like that clearly belongs to an if-else statement.
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    IMO, it is matter of choice! in java1.4 and earlier release ternary operator is not much flexible , so people tends to use if-else.

    for example below code wont compile in java1.4 . because ternary required 2nd or 3rd operand should be subtype of other one.

    but in java1.5, ternary operator is flexible . but still the operands must return something. you cant replace below if-else construct with ternary!

     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    so basically , there is no performance gain , by using ternary operator ,
    but effective java is claiming that using "?" ternery operator increase performance becuase compiler has to compile less code
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:i guess conditional operator "?" is similar to " if else block " ?


    Similar, but not equivalent. An if/else statement is not an expression and does not produce a result value. Use of this operator gives you an expression with a value.

    if yes , then why should we use "?" instead of " if else " which is much more simple to read & understand ?


    That operator is more compact and concise than if/else, and whether if/else is "much more simple to read and understand" is entirely subjective. When used properly, I find the ternary operator to be easier to read and understand.

    note : i know that it can be used to reduce the lines of code , so please give some more reasons why i should use this "?" operator


    You should use it in exactly the same situations I do. Namely, use it when you find it is the clearest way to express your thoughts in Java code.

     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64967
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:

    so basically , there is no performance gain , by using ternary operator ,
    but effective java is claiming that using "?" ternery operator increase performance becuase compiler has to compile less code

    This is all completely irrelevant. You should use whichever makes for the clearest code, not some infinitesimal performance difference.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:

    so basically , there is no performance gain , by using ternary operator ,
    but effective java is claiming that using "?" ternery operator increase performance becuase compiler has to compile less code


    Did they really say that? Or did they mention "less code" and you're assuming that means "performance gain"?

    The benefit of "less code" is for humans, not for the computer. Less code makes it easier for us to read. While it's true that less code will generally compile faster than more code, that is pointless for 2 reasons. 1) It's compile time only. Performance is a runtime issue. 2) The compile time difference between the ternary operator and if/else, or between any two different ways of expressing the same idea, will be so small that you'll never notice it unless you're doing millions of these things and little else in your code.

    So forget performance here. It has absolutely NOTHING to do with when to choose or not choose this operator.
     
    AbdulRab Khan
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Using ternary conditional operator just saves lines of code, that's the only use I guess for this operator. Used to write powerful compact code in few lines as possible.

     
    Matthew Brown
    Bartender
    Posts: 4567
    8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:Less code makes it easier for us to read.

    Sometimes. I've used perl too much to believe that's true in the general case.

    I agree with the general sentiment, though. Use the version that's clearest to someone using it. Often that will be using a ternary operator, but they can get difficult to follow when the expressions inside them get too complex.
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10422
    63
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    David Phluphy wrote:This means that conditions can be inlined into expressions, unlike with if statements, as shown here using C syntax:

    Strangely enough, I used to use a language that allowed you to do exactly what you coded in line 2. I rather liked it actually.

    @Naved: Like a lot of things in Java, the background of the ternary operator is in C, and it was probably adopted as being "familiar" to people coming from that arena.
    As it happens, in C it was also more efficient - as were all sorts of things that we tend to frown on in Java, such as embedded increments - because it produced tighter machine code. But these days, with the explosion of optimizing compilers, it may well no longer be the case (even in C).

    Jeff is quite right: use it for clarity, not for performance. And if you don't like it, don't use it at all. As long as what you do write is clear, nobody is going to complain.

    Winston
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Matthew Brown wrote:
    Jeff Verdegan wrote:Less code makes it easier for us to read.

    Sometimes. I've used perl too much to believe that's true in the general case.


    Yeah, I should have qualified that with "often" or "can" or something.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    AbdulRab Khan wrote:Using ternary conditional operator just saves lines of code, that's the only use I guess for this operator. Used to write powerful compact code in few lines as possible.



    Not exactly. It is the only language-level construct that let's us express that kind of choice as an expression, which in turn has the side-effect of being able to write less code.
     
    James Boswell
    Bartender
    Posts: 1051
    5
    Chrome Eclipse IDE Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Compare these 2 pieces of code.





    I much prefer the single line style.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic