aspose file tools*
The moose likes Java in General and the fly likes why to use conditional operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why to use conditional operator " ? "  ?  " Watch "why to use conditional operator " ? "  ?  " New topic
Author

why to use conditional operator " ? " ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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


  • The Only way to learn is ...........do!
    Visit my blog http://inaved-momin.blogspot.com/
    David Phluphy
    Greenhorn

    Joined: Sep 09, 2010
    Posts: 26
    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
    Bartender

    Joined: May 31, 2011
    Posts: 1776
    Nice read - http://www.coderanch.com/t/391402/java/java/Ternary-operator-if-condn

    Bit complicated nested ternary - http://www.coderanch.com/t/391402/java/java/Ternary-operator-if-condn
    Martin Vajsar
    Sheriff

    Joined: Aug 22, 2010
    Posts: 3610
        
      60

    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

    Joined: Jan 28, 2008
    Posts: 5575

    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

    Joined: Jul 03, 2011
    Posts: 692


    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

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Jan 10, 2002
    Posts: 61424
        
      67

    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.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Nov 11, 2011
    Posts: 7
    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

    Joined: Apr 06, 2010
    Posts: 4422
        
        8

    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

    Joined: Mar 17, 2011
    Posts: 8008
        
      22

    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


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Nov 09, 2011
    Posts: 1030
        
        5

    Compare these 2 pieces of code.





    I much prefer the single line style.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: why to use conditional operator " ? " ?