| Developer's Daily | Java Education |
| front page | java | perl | unix | DevDirectory |
Introduction
Interested in saying a lot while writing a little? In a single
line of code, Java's ternary operator let's you assign a
value to a variable based on one or more boolean decisions. Think
of this operator as a potential alternative to the if-then-else syntax.
Although it may be a little confusing when you first see it, the ternary
operator lets you make powerful decisions with a minimal amount of coding.
A sample min/max comparison
A common use of the ternary operator (also called the conditional operator) is to assign the minimum (or maximum) value of two variables to a third variable, essentially replacing a Math.min(a,b) or Math.max(a,b) method call.
The ternary code that assigns the minimum of two variables, a and b, to a third variable named minVal is:
In this code, if the variable a is less than b, minVal is assigned the value of a; otherwise, minVal is assigned the value of b. Note that the parentheses in this example are optional, but I use them to improve the readability of the code.
Also notice that the ternary operator is an expression that returns a value based on the conditional phrase it evaluates. This is different than the if-then-else syntax, which executes a statement block based on it's conditional phrase.
In performance tests on a Sun Solaris workstation, the ternary operator
was four times faster than the Math.min() method when comparing
float variables, and twice as fast when comparing integer variables.
In similar cases in your code, I recommend testing to determine if performance
improvements can be achieved. As the old saying goes, your performance
may vary.
Other ternary uses
In the ternary decision-making process, once a decision is made, you can return anything you want. In the case of a law-enforcement officer algorithm, the following code compares two numeric values and returns a boolean, to decide who gets a speeding ticket:
As a final example, note that the comparison portion of the ternary operator can contain complex if-then comparisons. The following code for a self-controlled car makes a fairly complex decision about when to stop, based on a series of boolean values:
// ... other code activity here ...
// decide when to stop the car
stopCar = lightIsRed || trafficIsStopped ||
outOfGas || wallInFront ? true : false;
In this case, if a traffic light is red, or traffic is stopped, or we're
out of gas, or there's a wall in front of us, we should set stopCar
to true. If not, just keep driving.
Conclusion
Once you get used to the slightly unusual syntax, the ternary operator
can help you make powerful decisions with a minimal amount of coding.
I find that it's a perfect fit where short true/false decisions lead to
the assignment of a value to a single variable. You'll also find
this operator in the source code of the class libraries of many vendors,
so it's good to have at least a modest understanding of this unusual operator.
Note: This article first appeared in ZD Journals JBuilder Journal, and is reprinted here with their permission. The article author now works at Developer's Daily.
Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.