6

Possible Duplicate:
What is the Java ?: operator called and what does it do?

hi, may i know what is the java ?: operator called, i am trying to find information on how it works but i do not know what is it called, typing ?: in google dont give a correct result.

Community
  • 1
  • 1
sutoL
  • 1,737
  • 10
  • 27
  • 48
  • Duplicate of [What is the Java ?: operator called and what does it do?](http://stackoverflow.com/questions/798545/what-is-the-java-operator-called-and-what-does-it-do) – James McNellis Jun 21 '10 at 14:51

6 Answers6

25

It's the conditional operator.

Some people call it the ternary operator, but that's really just saying how many operands it has. In particular, a future version of Java could (entirely reasonably) introduce another ternary operator - whereas the name of the operator is the conditional operator.

See section 15.25 of the language specification:

The conditional operator ? : uses the boolean value of one expression to decide which of two other expressions should be evaluated.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
12

ternary is the word you are looking for.

MJB
  • 7,639
  • 2
  • 31
  • 41
  • 5
    That's a description of how many operands it has... not the *name* of the operator. – Jon Skeet Jun 21 '10 at 14:52
  • Granted, searching 'ternary operator' instead of 'conditional operator' on Google works well, since you get fewer pages on `&&` and `||`. – Justin Ardini Jun 21 '10 at 14:54
  • @Jon: I know that is merely the description, but I was using the "what works best in google" logic. I agree I didn't properly answer the question of "what is it called", rather the implied question of "how do I look it up." But I have to admit that I don't think I deserved that many upvotes. – MJB Jun 21 '10 at 15:05
  • 4
    The more we encourage people to call it by its actual name, the better the Google results will get :) I'd get a perverse sense of satisfaction if Java *did* introduce another ternary operator, admittedly... – Jon Skeet Jun 21 '10 at 15:10
  • @Jon: ok, I can buy that. I initially felt that "conditional" has so many other meanings that a google search would provide too many hits (22M) vs ternary (4M). That's why I answered that way, but I guess that is just enabling. And the difference is irrelevant at that point anyway. – MJB Jun 21 '10 at 15:13
  • I agree with @JonSkeet just because Google gives you a better/accurate result doesn't mean that it should be called with that name. – Sreekanth Karumanaghat Apr 05 '19 at 06:03
8

JLS 15.25 Conditional Operator ? :

The conditional operator ? : uses the boolean value of one expression to decide which of two other expressions should be evaluated.

JLS 15.28 Constant Expression

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

  • The ternary conditional operator ? :

Thus, the Java Language Specification officially calls it the (ternary) conditional operator.


Java Coding Conventions - Indentation

Here are three acceptable ways to format ternary expressions:

alpha = (aLongBooleanExpression) ? beta : gamma;  

alpha = (aLongBooleanExpression) ? beta
                                 : gamma;  

alpha = (aLongBooleanExpression)
        ? beta 
        : gamma;  
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
6

This is known as the ternary or conditional operator (depending on who you ask)

It allows you to do single line conditional statements such as in this pseudocode

print a==1 ? 'a is one' : 'a is not one'

As Jon Skeet notes, it's proper name is the conditional operator, but it has 3 operands so is a ternary operator.

Neil Aitken
  • 7,856
  • 3
  • 41
  • 40
3

Do you mean for an if else statement? Look up the word ternery.

int x = 2;
String result = x > 1 ? "a" : "b";

equates to:

int x = 2;
String result = "";
if (x > 1) {
   result = "a";
} else {
   result = "b" ;
}
Corv1nus
  • 4,523
  • 1
  • 26
  • 37
1

it's called the conditional operator but very often called ternary operator (which is a class of operators all taking 3 operands however in Java only one such exits namely the conditional operator)

some times it's called the tertiary operator which is simply a language (english) usage error

Eventhouigh this is for c# the same applies to Java

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Rune FS
  • 21,497
  • 7
  • 62
  • 96