1. Technology

The ternary operator "?:" earns its name because it's the only operator to take three operands. It is a conditional operator that provides a shorter syntax for the if..then..else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned:

boolean expression ? value1 : value2

For example, the following if..then..else statement

boolean isHappy = true;
String mood = "";
if (isHappy == true)
{
  mood = "I'm Happy!";
}
else
{
  mood = "I'm Sad!";
}

can be reduced to one line using the ternary operator:

boolean isHappy = true;
String mood = (isHappy == true)?"I'm Happy!":"I'm Sad!";

Generally the code is easier to read when the if..then..else statement is written in full but sometimes the ternary operator can be a handy syntax shortcut.

Comments
July 1, 2009 at 1:29 pm
(1) ax says:

that one line should be more concise:

String mood = isHappy ? “I’m Happy!” : “I’m Sad!”;

you don’t have to compare a boolean with true or false, as evaluating it as an expression yields true or false already.

July 1, 2009 at 6:42 pm
(2) Paul Leahy says:

Yep, you’re right. And, if you look at the condition in the if statement, that too can be reduced to being just

if (isHappy)

I think having the expressions written so that beginners can see how the logic is being evaluated can make them a little easier to understand. But, I’ll add a note to the end of the glossary entry with the concise version too.

September 17, 2010 at 11:09 am
(3) вавыфавыфа says:

until you need to debug that sht

August 23, 2011 at 9:44 pm
(4) Roark says:

Debugging it is not so difficult if you do understand it. Making things clear is important but being concise does produce faster and better code.

November 6, 2011 at 5:07 am
(5) Frank says:

Roark, more concise code does not run any faster. You’re writing in a high level language like Java so your code will be readable. The byte-compiled if/else and ternary will ultimately run the same, no matter how many ‘lines of code’ you use.

February 14, 2012 at 9:43 am
(6) Didar says:

Great explanation….thanks a lot

September 21, 2012 at 8:10 am
(7) padisah says:

Of course, the ternary won’t be faster because it’s code is shorter.

Readability depends on the complexity of the surrounding code. I like the ternary, because it avoids the curly brace jungle. The line structure can be split to 2 rows, so it stay readable.

October 19, 2012 at 10:13 am
(8) John says:

I never understood the complaint that the ternary operator was harder to debug…

debugging a one line assignment is far simpler to me.

December 5, 2012 at 3:58 pm
(9) Cynthia says:

Ternary operations may seem nice to the oh-so-clever original author but these are horrid for the poor sap stuck maintaining your undocumented creation. Eighty percent of the cost of any software is it’s maintenance so clarity & comments really count in the long run! If you’ve just got to use them, please add parentheses around each condition and a descriptive comment for the next programmer.

February 8, 2013 at 2:31 pm
(10) Bow-wowow says:

And for crying out loud, don’t nest ternary operators.

May 17, 2013 at 10:27 am
(11) The Internet Cometh says:

Awesome explanation! The ternary operator has confused me a bit, but I now understand it completely thanks to your article here. :)

Cheers!

July 12, 2013 at 1:10 pm
(12) James Thomas says:

The ternary operator has been around so long it is found in Java code extensively. I sympathize with Cynthia’s basic premise but I would not discourage use of the ternary operator. With the explanation above, anyone should be able to discern easily what it is doing. Even without the explanation above, a professional Java programmer should be familiar with the basic operators in Java – and this is one.

August 21, 2013 at 10:25 am
(13) Brindha says:

I guess the java program is not very clear didn’t get much out of it though….i’m sry but if anyone could expalin it better, i’m all ears

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.