What is the ternary (?:) operator?
Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.
What is the ternary (?:) operator?
Rating:
The ?: operator is a conditional operator that is used as a short hand for an if-else statement in some cases. It is also known as ternary operator because it takes three operands. The general form of the operator is as follows:
where, opr1 can be any expression that evaluates to a boolean value. Both opr2 and opr3 are required to return values of the same type. If opr1 evaluates to true, opr2 is evaluated. Otherwise, opr3 is evaluated.
The general form of the operator is equivalent to the following if-else statement:
opr2
}
else{
opr3
}
Rating:
Was this information helpful?
Other articles
- What is the DateFormat class?
- What is modifier?
- What is a split() function?
- What is the Set interface?
- What is the Map interface?