Java“?:”运算符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3085681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:18:07  来源:igfitidea点击:

Java "? :" operator?

javaternary-operator

提问by sutoL

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

可能的重复:
Java 是什么?:操作符被调用,它有什么作用?

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.

嗨,我可以知道 java 是什么吗?:操作符被称为,我正在尝试查找有关它如何工作的信息,但我不知道它叫什么,键入 ?: 在 google 中没有给出正确的结果。

采纳答案by Jon Skeet

It's the conditionaloperator.

这是条件运算符。

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

有些人称它为三元运算符,但这实际上只是说它有多少个操作数。特别是,Java 的未来版本可以(完全合理地)引入另一个三元运算符 - 而运算符的名称是条件运算符。

See section 15.25 of the language specification:

请参阅语言规范的第 15.25 节

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

条件运算符 ? : 使用一个表达式的布尔值来决定应该评估其他两个表达式中的哪一个。

回答by MJB

ternary is the word you are looking for.

三元是你要找的词。

回答by Neil Aitken

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.

正如 Jon Skeet 所指出的,它的正确名称是条件运算符,但它有 3 个操作数,因此三元运算符也是如此。

回答by polygenelubricants

JLS 15.25 Conditional Operator ? :

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

JLS 15.28 Constant Expression

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

  • The ternary conditional operator ? :

JLS 15.25 条件运算符?:

条件运算符 ? :使用boolean一个表达式的值来决定应评估其他两个表达式中的哪一个。

JLS 15.28 常量表达式

编译时常量表达式是表示原始类型的值或String不会突然完成并且仅使用以下内容组成的表达式:

  • 三元条件运算符 ? :

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

因此,Java 语言规范正式将其称为(三元)条件运算符。



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;  

Java 编码约定 - 缩进

以下是格式化三元表达式的三种可接受的方式:

alpha = (aLongBooleanExpression) ? beta : gamma;  

alpha = (aLongBooleanExpression) ? beta
                                 : gamma;  

alpha = (aLongBooleanExpression)
        ? beta 
        : gamma;  

回答by Corv1nus

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

你的意思是 if else 语句吗?查找 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" ;
}

回答by Rune FS

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)

它被称为条件运算符,但通常称为三元运算符(这是一类都采用 3 个操作数的运算符,但在 Java 中只有一个这样的退出,即条件运算符)

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

有时它被称为三级运算符,它只是一种语言(英语)使用错误

Eventhouigh thisis for c# the same applies to Java

Eventhouigh适用于 c#,同样适用于 Java