爪哇”? :”操作员?

发布于 2024-09-06 14:46:10 字数 293 浏览 9 评论 0原文

可能的重复:
什么是 Java ?: 运算符调用以及什么可以吗?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

傲性难收 2024-09-13 14:46:10

这是条件运算符。

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

请参阅语言规范的第 15.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.

唠甜嗑 2024-09-13 14:46:10

三元是您要找的词。

ternary is the word you are looking for.

谎言 2024-09-13 14:46:10

JLS 15.25 条件运算符 ? :

条件运算符? :使用一个表达式的boolean值来决定应该计算另外两个表达式中的哪一个。

JLS 15.28 常量表达式

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

  • 三元条件运算符? :

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


Java 编码约定 - 缩进

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

alpha = (aLongBooleanExpression) ?贝塔:伽玛;  

alpha = (aLongBooleanExpression) ?贝塔
                                 :伽玛;  

alpha = (aLongBooleanExpression)
        ?贝塔 
        :伽玛;  

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;  
忱杏 2024-09-13 14:46:10

这被称为三元或条件运算符(取决于你问的是谁)

它允许你执行单行条件语句,例如在这个伪代码中,正如

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

Jon Skeet 所说,它的正确名称是条件运算符,但它有 3 个操作数,所以三元运算符。

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.

羅雙樹 2024-09-13 14:46:10

你的意思是 if else 语句吗?查一下“ternery”这个词。

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

等于:

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

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" ;
}
停顿的约定 2024-09-13 14:46:10

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

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

事件这个< /a> 适用于 c#,同样适用于 Java

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文