Java operator ??:?

Possible duplicate:
What is a Java statement ?: and what does it do?

hi, can I know what java is ?: operator called, I'm trying to find information on how this works, but I don’t know what he called by typing ?: google do not give the correct result.

+4
source share
6 answers

This is a conditional statement.

Some call it the ternary operator, but it really just says how many operands it has. In particular, a future version of Java could (quite reasonably) introduce another ternary operator, while the operator name is a conditional operator.

See section 15.25 of the language specification :

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

+20
source

trernary is the word you are looking for.

+12
source

JLS 15.25 Conditional statement ?:

Conditional statement ? : ? : uses the boolean value of one expression to determine which of the other two expressions should be evaluated.

JLS 15.28 Constant Expression

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

  • Ternary conditional operator ? : ? :

Thus, the Java language specification officially calls it a (triple) conditional statement.


Java coding conventions - indentation

Here are three acceptable ways to format 3D expressions:

 alpha = (aLongBooleanExpression) ? beta : gamma; alpha = (aLongBooleanExpression) ? beta : gamma; alpha = (aLongBooleanExpression) ? beta : gamma; 
+8
source

This is called a ternary or conditional statement (depending on who you ask)

This allows you to make single-line conditional expressions, for example, in this pseudocode

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

As John Skeet notes, his own name is a conditional operator, but it has 3 operands, so this is a triple operator.

+6
source

Do you mean the expression if if? Look at the word ternery.

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

corresponds to:

 int x = 2; String result = ""; if (x > 1) { result = "a"; } else { result = "b" ; } 
+1
source

it is called a conditional operator, but very often called a ternary operator (which is a class of operators, everyone takes 3 operands, but in Java there is only one such output, namely the conditional operator)

several times it was called a tertiary operator, which is simply a mistake in using the language (English)

Eventhouigh this for C # the same applies to Java

+1
source

Source: https://habr.com/ru/post/1313401/