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.
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.
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.
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.
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.
Isn't it funny how there's always time and money enough to do it WRONG?