Java:在println (boolean ?真正“打印”:“打印错误”)(复制)

时间:2022-11-13 16:26:24

This question already has an answer here:

这个问题已经有了答案:

I came across this syntax:

我遇到这种语法:

System.out.println(boolean_variable ? "print true": "print false");
  • What is this syntax with two dots : called?
  • 有两个点的语法是什么?
  • Where can I find info about it?
  • 我在哪里可以找到有关它的信息?
  • Does it work just for booleans or is it implemented in other different ways?
  • 它只对布尔人有效还是以其他方式实现?

11 个解决方案

#1


54  

? : is the conditional operator. (It's not just the : part - the whole of the method argument is one usage of the conditional operator in your example.)

吗?:是条件算子。(它不只是:part—方法参数的全部是您的示例中条件运算符的一种用法)。

It's often called the ternary operator, but that's just an aspect of its nature - having three operands - rather than its name. If another ternary operator is ever introduced into Java, the term will become ambiguous. It's called the conditional operator because it has a condition (the first operand) which then determines which of the other two operands is evaluated.

它通常被称为三元运算符,但这只是它的性质的一个方面——有三个操作数——而不是它的名字。如果在Java中引入另一个三元运算符,这个术语将变得不明确。它被称为条件运算符,因为它有一个条件(第一个操作数),这个条件决定了其他两个操作数中的哪一个被求值。

The first operand is evaluated, and then either the second or the third operand is evaluated based on whether the first operand is true or false... and that ends up as the result of the operator.

计算第一个操作数,然后根据第一个操作数是真还是假来计算第二个或第三个操作数…这是算符的结果。

So something like this:

所以这样的:

int x = condition() ? result1() : result2();

is roughly equivalent to:

相当于:

int x;
if (condition()) {
    x = result1();
} else {
    x = result2();
}  

It's important that it doesn't evaluate the other operand. So for example, this is fine:

重要的是它不计算另一个操作数。举个例子,这很好

String text = getSomeStringReferenceWhichMightBeNull();
int usefulCharacters = text == null ? 0 : text.length();

#2


20  

It's the conditional operator, often called ternary operator because it has 3 operands: An example would be:

它是条件运算符,通常被称为三元运算符因为它有三个操作数:一个例子是:

int foo = 10;
int bar = foo > 5 ? 1 : 2; // will be 1
int baz = foo > 15 ? 3 : 4; // will be 4

So, if the boolean expression evaluates to true, it will return the first value (before the colon), else the second value (after the colon).

因此,如果布尔表达式的计算值为true,它将返回第一个值(冒号之前),否则返回第二个值(冒号之后)。

You can read the specifics in the Java Language Specification, Chapter 15.25 Conditional Operator ?

您可以阅读Java语言规范15.25章条件运算符中的细节吗?

#3


4  

It's a ternary operator, meaning that instead of having two operands like many other operators, it has three. Wikipedia on Ternary Operation and how it's used in Java. What it boils down to: the boolean operation (or just a variable) is evaluated. If it evaluates to true, the operator returns the value / executes the code before the :, otherwise the one after it.

它是一个三元运算符,意思是它不是像许多其他运算符那样有两个操作数,而是有三个。*关于三元操作以及如何在Java中使用。归结为:布尔运算(或只是一个变量)被评估。如果计算结果为true,操作符将返回值/在:之前执行代码,否则返回其后的代码。

#4


2  

That's an if statement.

这是一个if语句。

What's to the left of ? is the condition, what's between the ? and : is the result if the condition is true, and what's to the right of : is the result if the condition is false.

左边是什么?条件是什么?如果条件为真,则结果为:如果条件为假,则结果为。

#5


2  

This is ternary operator (http://en.wikipedia.org/wiki/?:). It can be used anywhere when you need a small if expression.

这是ternary操作符(http://en.wikipedia.org/wiki/?)当您需要一个小if表达式时,它可以在任何地方使用。

#6


2  

For your questions:

对于你的问题:

  1. The ?: (both characters together) are called conditional operator (or ternary operator). Only both together will work.
  2. (两个字符一起)称为条件运算符(或三元运算符)。只有两者结合才能奏效。
  3. Search for java ternery operator
  4. 搜索java ternery操作符
  5. It only works for boolean
  6. 它只适用于布尔型

In principle the ternery operator is a shortened if/else. The boolean will be the condition to the if, the part between ? and : is the if branch and the part after this is the else branch.

原则上,ternery运算符是if/else的缩写。布尔值将是if的条件,中间的部分?和:是if分支,后面的部分是else分支。

Please note that the return type of the conditional operator is determined by the first branch.

请注意,条件运算符的返回类型是由第一个分支决定的。

#7


1  

It's the ternary operator and it works with booleans. It can be used as a shorthand for if-else in some cases, but shouldn't be used for too complicated things as it can be difficult to read.

它是三元运算符,和布尔运算。在某些情况下,它可以作为if-else的简写,但不能用于太复杂的事情,因为它可能很难读懂。

An example would be assigning value to a variable depending on a condition:

一个例子是根据条件为变量赋值:

String message = doOperation() ? "Success" : "Error occurred";
System.out.println(message);

In this case, if doOperation returns a boolean telling whether it succeeded or not, the message to be shown can be assigned on a single line.

在这种情况下,如果doOperation返回一个布尔值,告诉它是否成功,那么显示的消息可以分配到一行上。

Please note that this example does not represent good programming practices.

请注意,这个示例并不代表好的编程实践。

#8


1  

Its ternary operator.

三元操作符。

The ternary operator or ?, is a shorthand if else statement. It can be used to evaluate an expression and return one of two operands depending on the result of the expression.

三元运算符是if else语句的简写形式。它可以用于计算表达式,并根据表达式的结果返回两个操作数中的一个。

boolean b = true;
String s = ( b == true ? "True" : "False" );

This will set the value of the String s according to the value of the boolean b. This could be written using an if else statement like this:

这将根据布尔b的值设置字符串s的值。

boolean b = true;
String s;
if(b == true){
    s = "True";
}else{
    s = "False";
}

#9


1  

Its a short form of if-else statement.

这是if-else语句的一种简短形式。

It works in this way

它是这样运作的。

(yourCondition ? STATEMENT1 : STATEMENT2)
  • The compiler checks for the condition.
  • 编译器检查条件。
  • IF it returns TRUE then STATEMENT1 will be executed.
  • 如果返回TRUE,那么将执行STATEMENT1。
  • ELSE STATEMENT2 will be executed.
  • 否则将执行语句2。

#10


0  

The question mark followed by a colon (two dots) is a ternary operator usually called inline if.

问号后面跟着冒号(两个点)是一个三元运算符,通常称为内联if。

In this case it returns a string depending on the value of boolean_variable.

在本例中,它根据boolean_variable的值返回一个字符串。

http://en.wikipedia.org/wiki/%3F:

http://en.wikipedia.org/wiki/%3F:

#11


0  

See here. The ternary operator is similar to an if expression but differs in that it is an expression - it has a return value, while if expressions don't. Sometimes you want to use it to make your code a little less cluttered.

在这里看到的。三元运算符类似于if表达式,但不同的是它是一个表达式——它有一个返回值,而if表达式没有。有时候你想用它来使你的代码不那么杂乱。

#1


54  

? : is the conditional operator. (It's not just the : part - the whole of the method argument is one usage of the conditional operator in your example.)

吗?:是条件算子。(它不只是:part—方法参数的全部是您的示例中条件运算符的一种用法)。

It's often called the ternary operator, but that's just an aspect of its nature - having three operands - rather than its name. If another ternary operator is ever introduced into Java, the term will become ambiguous. It's called the conditional operator because it has a condition (the first operand) which then determines which of the other two operands is evaluated.

它通常被称为三元运算符,但这只是它的性质的一个方面——有三个操作数——而不是它的名字。如果在Java中引入另一个三元运算符,这个术语将变得不明确。它被称为条件运算符,因为它有一个条件(第一个操作数),这个条件决定了其他两个操作数中的哪一个被求值。

The first operand is evaluated, and then either the second or the third operand is evaluated based on whether the first operand is true or false... and that ends up as the result of the operator.

计算第一个操作数,然后根据第一个操作数是真还是假来计算第二个或第三个操作数…这是算符的结果。

So something like this:

所以这样的:

int x = condition() ? result1() : result2();

is roughly equivalent to:

相当于:

int x;
if (condition()) {
    x = result1();
} else {
    x = result2();
}  

It's important that it doesn't evaluate the other operand. So for example, this is fine:

重要的是它不计算另一个操作数。举个例子,这很好

String text = getSomeStringReferenceWhichMightBeNull();
int usefulCharacters = text == null ? 0 : text.length();

#2


20  

It's the conditional operator, often called ternary operator because it has 3 operands: An example would be:

它是条件运算符,通常被称为三元运算符因为它有三个操作数:一个例子是:

int foo = 10;
int bar = foo > 5 ? 1 : 2; // will be 1
int baz = foo > 15 ? 3 : 4; // will be 4

So, if the boolean expression evaluates to true, it will return the first value (before the colon), else the second value (after the colon).

因此,如果布尔表达式的计算值为true,它将返回第一个值(冒号之前),否则返回第二个值(冒号之后)。

You can read the specifics in the Java Language Specification, Chapter 15.25 Conditional Operator ?

您可以阅读Java语言规范15.25章条件运算符中的细节吗?

#3


4  

It's a ternary operator, meaning that instead of having two operands like many other operators, it has three. Wikipedia on Ternary Operation and how it's used in Java. What it boils down to: the boolean operation (or just a variable) is evaluated. If it evaluates to true, the operator returns the value / executes the code before the :, otherwise the one after it.

它是一个三元运算符,意思是它不是像许多其他运算符那样有两个操作数,而是有三个。*关于三元操作以及如何在Java中使用。归结为:布尔运算(或只是一个变量)被评估。如果计算结果为true,操作符将返回值/在:之前执行代码,否则返回其后的代码。

#4


2  

That's an if statement.

这是一个if语句。

What's to the left of ? is the condition, what's between the ? and : is the result if the condition is true, and what's to the right of : is the result if the condition is false.

左边是什么?条件是什么?如果条件为真,则结果为:如果条件为假,则结果为。

#5


2  

This is ternary operator (http://en.wikipedia.org/wiki/?:). It can be used anywhere when you need a small if expression.

这是ternary操作符(http://en.wikipedia.org/wiki/?)当您需要一个小if表达式时,它可以在任何地方使用。

#6


2  

For your questions:

对于你的问题:

  1. The ?: (both characters together) are called conditional operator (or ternary operator). Only both together will work.
  2. (两个字符一起)称为条件运算符(或三元运算符)。只有两者结合才能奏效。
  3. Search for java ternery operator
  4. 搜索java ternery操作符
  5. It only works for boolean
  6. 它只适用于布尔型

In principle the ternery operator is a shortened if/else. The boolean will be the condition to the if, the part between ? and : is the if branch and the part after this is the else branch.

原则上,ternery运算符是if/else的缩写。布尔值将是if的条件,中间的部分?和:是if分支,后面的部分是else分支。

Please note that the return type of the conditional operator is determined by the first branch.

请注意,条件运算符的返回类型是由第一个分支决定的。

#7


1  

It's the ternary operator and it works with booleans. It can be used as a shorthand for if-else in some cases, but shouldn't be used for too complicated things as it can be difficult to read.

它是三元运算符,和布尔运算。在某些情况下,它可以作为if-else的简写,但不能用于太复杂的事情,因为它可能很难读懂。

An example would be assigning value to a variable depending on a condition:

一个例子是根据条件为变量赋值:

String message = doOperation() ? "Success" : "Error occurred";
System.out.println(message);

In this case, if doOperation returns a boolean telling whether it succeeded or not, the message to be shown can be assigned on a single line.

在这种情况下,如果doOperation返回一个布尔值,告诉它是否成功,那么显示的消息可以分配到一行上。

Please note that this example does not represent good programming practices.

请注意,这个示例并不代表好的编程实践。

#8


1  

Its ternary operator.

三元操作符。

The ternary operator or ?, is a shorthand if else statement. It can be used to evaluate an expression and return one of two operands depending on the result of the expression.

三元运算符是if else语句的简写形式。它可以用于计算表达式,并根据表达式的结果返回两个操作数中的一个。

boolean b = true;
String s = ( b == true ? "True" : "False" );

This will set the value of the String s according to the value of the boolean b. This could be written using an if else statement like this:

这将根据布尔b的值设置字符串s的值。

boolean b = true;
String s;
if(b == true){
    s = "True";
}else{
    s = "False";
}

#9


1  

Its a short form of if-else statement.

这是if-else语句的一种简短形式。

It works in this way

它是这样运作的。

(yourCondition ? STATEMENT1 : STATEMENT2)
  • The compiler checks for the condition.
  • 编译器检查条件。
  • IF it returns TRUE then STATEMENT1 will be executed.
  • 如果返回TRUE,那么将执行STATEMENT1。
  • ELSE STATEMENT2 will be executed.
  • 否则将执行语句2。

#10


0  

The question mark followed by a colon (two dots) is a ternary operator usually called inline if.

问号后面跟着冒号(两个点)是一个三元运算符,通常称为内联if。

In this case it returns a string depending on the value of boolean_variable.

在本例中,它根据boolean_variable的值返回一个字符串。

http://en.wikipedia.org/wiki/%3F:

http://en.wikipedia.org/wiki/%3F:

#11


0  

See here. The ternary operator is similar to an if expression but differs in that it is an expression - it has a return value, while if expressions don't. Sometimes you want to use it to make your code a little less cluttered.

在这里看到的。三元运算符类似于if表达式,但不同的是它是一个表达式——它有一个返回值,而if表达式没有。有时候你想用它来使你的代码不那么杂乱。