java 三元运算符 return 函数结果
java ternary operator return function result
我只是想知道是否可以创建这样的东西:
String variable = "name of the variable I'm checking";
for (int i = 0; i < 16; i++)
{ // if it's possible what type should the check variable be?
check = (array[0].equals("value1") || array[0].equals("value2") ?
"some value or null?" : throwException(i, variable);
}
public void throwException(int index, String name) throws TelegrammException
{
throw new TelegrammException("Wrong telegramm format at position: "
+ index + "; Name: " + name);
}
如果这是不可能的,你能建议一个好的做法来做类似的事情吗?
技术上 是,如果您将 throwException
方法的 return 类型更改为 String
。
public String throwException(int index, String name) throws TelegrammException
但是,使用一种表面上 return 是一个实际上总是抛出字符串的方法是非常不寻常的,并且几乎肯定会使您的代码的未来读者感到困惑。表达意图的惯用方式是完全不使用三元表达式:
if (array[0].equals("value1") || array[0].equals("value2"))
{
check = "some value or null?";
}
else
{
throwException(i, variable);
}
不,三元运算符不可能。至少在 Java 中没有。两个表达式都应该解析为 return 相同的东西。否则编译器会发出错误。除了 throwing 表达式,你的第二个表达式没有 returning 任何东西。这样不行。
From the JLS,关于三元运算符:
The first expression must be of type boolean
or Boolean
, or a compile-time error occurs.
It is a compile-time error for either the second or the third operand expression to be an invocation of a void
method.
你应该考虑用传统方法来做。
for (int i = 0; i < 16; i++)
{ // if it's possible what type should the check variable be?
if (array[0].equals("value1") || array[0].equals("value2"))
{
check = "some value or null?";
} else {
throwException(i, variable);
}
}
当然:制作你的方法"return" String
:
public String throwException(int index, String name) throws TelegrammException
{
throw new TelegrammException("Wrong telegramm format at position: "
+ index + "; Name: " + name);
}
现在,这实际上从未 return 成为 String
因为它没有正常完成。但是您现在可以在条件表达式中使用它:
String result = condition ? "Something" : throwException(index, name);
但是:这是对语法的滥用。只需坚持使用普通的旧 if 语句:
if (condition) {
result = "Something";
} else {
throwException(index, name);
}
您可能还想考虑制作方法的 return 类型 TelegrammException
:
public TelegrammException throwException(int index, String name) throws ...
这使您可以 throw
在呼叫站点:
throw throwException(index, name);
这允许您向编译器(以及阅读您的代码的人)指示执行不会超出那里,这可能会帮助您满足明确的 assignment/return 值要求。
我只是想知道是否可以创建这样的东西:
String variable = "name of the variable I'm checking";
for (int i = 0; i < 16; i++)
{ // if it's possible what type should the check variable be?
check = (array[0].equals("value1") || array[0].equals("value2") ?
"some value or null?" : throwException(i, variable);
}
public void throwException(int index, String name) throws TelegrammException
{
throw new TelegrammException("Wrong telegramm format at position: "
+ index + "; Name: " + name);
}
如果这是不可能的,你能建议一个好的做法来做类似的事情吗?
技术上 是,如果您将 throwException
方法的 return 类型更改为 String
。
public String throwException(int index, String name) throws TelegrammException
但是,使用一种表面上 return 是一个实际上总是抛出字符串的方法是非常不寻常的,并且几乎肯定会使您的代码的未来读者感到困惑。表达意图的惯用方式是完全不使用三元表达式:
if (array[0].equals("value1") || array[0].equals("value2"))
{
check = "some value or null?";
}
else
{
throwException(i, variable);
}
不,三元运算符不可能。至少在 Java 中没有。两个表达式都应该解析为 return 相同的东西。否则编译器会发出错误。除了 throwing 表达式,你的第二个表达式没有 returning 任何东西。这样不行。
From the JLS,关于三元运算符:
The first expression must be of type
boolean
orBoolean
, or a compile-time error occurs.It is a compile-time error for either the second or the third operand expression to be an invocation of a
void
method.
你应该考虑用传统方法来做。
for (int i = 0; i < 16; i++)
{ // if it's possible what type should the check variable be?
if (array[0].equals("value1") || array[0].equals("value2"))
{
check = "some value or null?";
} else {
throwException(i, variable);
}
}
当然:制作你的方法"return" String
:
public String throwException(int index, String name) throws TelegrammException
{
throw new TelegrammException("Wrong telegramm format at position: "
+ index + "; Name: " + name);
}
现在,这实际上从未 return 成为 String
因为它没有正常完成。但是您现在可以在条件表达式中使用它:
String result = condition ? "Something" : throwException(index, name);
但是:这是对语法的滥用。只需坚持使用普通的旧 if 语句:
if (condition) {
result = "Something";
} else {
throwException(index, name);
}
您可能还想考虑制作方法的 return 类型 TelegrammException
:
public TelegrammException throwException(int index, String name) throws ...
这使您可以 throw
在呼叫站点:
throw throwException(index, name);
这允许您向编译器(以及阅读您的代码的人)指示执行不会超出那里,这可能会帮助您满足明确的 assignment/return 值要求。