How do I read this return statement?
This is the first time I come across the use of ? -1 : and ? 0:1 . The example I've taken from Interface tutorial.
return (e1.number() < e2.number() ? -1 :
(e1.number() == e2.number() ? 0 : 1));
Can anyone tell me how do I read the statement. What is this style of writing called so that I can find out more about it for future use.
Thanks
ternary operator: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
It's the same as
if (e1.number() < e2.number()) {
return -1;
}
if (e1.number() == e2.number()) {
return 0;
}
return 1;
Kaj
I'm not 100% sure of this, as I'm not too familiar with that notation myself (I saw it in a tutorial somewhere, but I can't find it again, even though I looked), but I believe you should read it as:
if (e1.number() < e2.number()) {
return -1;
} else if (e1.number() == e2.number()) {
return 0;
} else {
return 1;
}
EDIT: Ah the TERNARY operator! That's what it is! Thanks for the link.
Message was edited by:
Djaunl
> I'm not 100% sure of this, as I'm not too familiar> with that notation myself (I saw it in a tutorial> somewhere, but I can't find it again, even though I> looked), but I believe you should read it as:No need for the else statements.
That is Java's only(?) ternary operator and it is a shorthand way of saying if...else.
If I have read it correctly, the statement says;
If e1.number() is less than e2.number() return -1.
Else
if e1.number() is equal to e2.number() return 0.
Else
return 1.
Basically there is a condition that is evaluated. That part of the statement is coded before the question mark(?). If the condition evaluates to the boolean value 'true' then the result of the expression will be value to the left of the colon (:) else if the condition evaluates to 'false' then the result of the expression will be the vlaue to the right of the colon.
condition ? value : value;
VERY CRUDELY
Thanks for all the replies. I'm off to read up more about it :)
> No need for the else statements.I see, thanks. Just wondering, I read in a thread awhile ago that the ternary operator is not recommended for use. Is this just because the code may not look as clear as it would if there were if/else statements, or is there another
> Just wondering, I read in a thread awhile ago that
> the ternary operator is not recommended for use. Is
> this just because the code may not look as clear as
> it would if there were if/else statements
Correct.
Some people say that you shouldn't use it because some programmers might not recognize what it is, and how it works (just as in this thread). But I don't think that you should lower the level of all programmers to the same level as the worst programmer. (But I still agree that it's very important to write code which is maintainable)
Kaj
Alright, that makes sense. I bet even if I used the ternary operator in my code frequently, it would still confuse me greatly if I had to debug/re-read that certain section; if/else just looks nicer :-).Thanks for the help.
Personally, I prefer the ternary operator for readability in some cases; e.g., very simple conditional assignments.~
Nested conditional operators can be hard to read and I would avoid that as I avoid any hard-to-read stuff, but there's nothing wrong with a simple use of one conditional:
obj.setValue(cool ? ok : nope);
I claim that's both cleaner and simpler than:
if (cool) {
obj.setValue(ok);
} else {
obj.setValue(nope);
}
Yes, after looking through some of the code I have written in the past I can see where the ternary operator could be used for simple conditionals for greater readability like yawmark and DrLaszlo have mentioned, although the ternary operator still looks confusing to me when I first see the statement. I assume that is due to my lack of experience with the operator however.
I'll definitely have to try to incorporate this into some code in the future just to get a feel for it; it does look rather nice for some operations.
Anyway, although I'm not the OP, thanks for all the replies to this thread, as they've been helpful to me.