Java 返回语句语法
返回语句中的符号“?”和“:”是什么意思Java 返回语句语法,java,Java,返回语句中的符号“?”和“:”是什么意思 public boolean isItBigger(BigInteger num1, Long num2) { return num1 == BigInteger.ONE || num2.intValue() > 0 ? true : false; } 我想我在if语句中也看到了它们。使用?和:是Java的(JLS 15.25)。表达式的结果 aBoolean ? expr1 : expr2 是expr1如果aBoole
public boolean isItBigger(BigInteger num1, Long num2) {
return num1 == BigInteger.ONE || num2.intValue() > 0 ? true : false;
}
我想我在if语句中也看到了它们。使用
?
和:
是Java的(JLS 15.25)。表达式的结果
aBoolean ? expr1 : expr2
是expr1
如果aBoolean
是true
,否则它是expr2
在这种情况下,可以不使用它,因为它是不必要的:
return num1 == BigInteger.ONE || num2.intValue() > 0;
使用
?
和:
是Java的(JLS 15.25)。表达式的结果
aBoolean ? expr1 : expr2
是expr1
如果aBoolean
是true
,否则它是expr2
在这种情况下,可以不使用它,因为它是不必要的:
return num1 == BigInteger.ONE || num2.intValue() > 0;
这是一个好主意。以下是等效的
if (x == y)
x = 2;
else
x = 3;
及
不过,您的示例代码很愚蠢。首先,他们检查表达式的计算结果是否为true。然后,如果是,则返回true。它们也可以返回表达式本身的结果,如下所示:
return num1 == BigInteger.ONE || num2.intValue() > 0;
这是一个好主意。以下是等效的
if (x == y)
x = 2;
else
x = 3;
及
不过,您的示例代码很愚蠢。首先,他们检查表达式的计算结果是否为true。然后,如果是,则返回true。它们也可以返回表达式本身的结果,如下所示:
return num1 == BigInteger.ONE || num2.intValue() > 0;
这称为条件/三元运算符
boolean-expression ? do-this-if-true : else-do-this
它是
if (boolean-expression) {
do-this
} else{
do-this
}
这称为条件/三元运算符
boolean-expression ? do-this-if-true : else-do-this
它是
if (boolean-expression) {
do-this
} else{
do-this
}
与返回相同(num1==biginger.ONE | | num2.intValue()>0)代码>是的,现在我明白了,谢谢大家的回复:)与
返回相同(num1==biginger.ONE | | num2.intValue()>0)代码>是的,现在我明白了,谢谢大家的回复:)我现在明白了,你是对的,不需要最后的真假表达。谢谢大家的回应。我现在明白了,你是对的,没有必要在最后说真假。谢谢大家的回复。