This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
First there was C, and it was good, mostly. Then was begat C++ and it was an abomination, somewhat. Then in an attempt to clean it up, we got Java. It was so clean and pure that they modified it. And again. And again. ... (has Java 7 been released, yet)?
This operator is an inheritance of C, but is mostly useful. It should only be used where it aids in code readability!
This is a fairly common idiom in the language:
So, there's a convenient shorthand:
Of course your example didn't help by explicitly putting parentheses around the condition to clarify the order of operations, and are surrounded by script cruft.
[ April 17, 2008: Message edited by: Bill Shirley ] [ April 17, 2008: Message edited by: Bill Shirley ]
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
There's also a case where the conditional operatior is essential: You cannot wrap calls to a super constructor in if-else-blocks, so if you want to decide at runtime which value to pass to a super constructor, you can do nothing but use the (condition ? x : y) expression. This is useful, for instance, if your constructor takes some Object as an argument and you want to pass a fallback to the super constructor in case the specified argument is null.