:"表示法设置短值-6ren"> :"表示法设置短值-我遇到了一个我不太明白的奇怪问题,但这不是我程序的障碍。我正在设置 short 的值变量基于条件的结果,如果我使用标准的 if/else,它工作正常,但如果我使用速记条件赋值,它就不能正常工作。 这是-6ren">
gpt4 book ai didi

java - 无法在 Java 中使用 "(expr)?:"表示法设置短值

转载 作者:行者123 更新时间:2023-11-29 07:48:05 25 4
gpt4 key购买 nike

我遇到了一个我不太明白的奇怪问题,但这不是我程序的障碍。我正在设置 short 的值变量基于条件的结果,如果我使用标准的 if/else,它工作正常,但如果我使用速记条件赋值,它就不能正常工作。

这是一个简单的例子:

public class ShortTest {
public static void main(String args []) {
int i = 0;
short s = 0;

// This always works
if( i < 100 )
s = 0;
else
s = 1;

// This causes an error
s = (i < 100)?1:0;
}
}

编译此代码会产生以下结果:

ShortTest.java:13: error: possible loss of precision
s = (i < 100)?1:0;
^
required: short
found: int
1 error

但是,如果您向 short 添加强制转换到 1 , 0 ,或两者兼而有之,那么它工作正常。所有这三个语句都将起作用:

s = (i < 100)?(short)1:0;

s = (i < 100)?1:(short)0;

s = (i < 100)?(short)1:(short)0;

这没什么大不了的,因为 if/else 没问题,但这让我抓狂。为什么不能从 int 转换这些值?至 short当他们处于速记条件时自动?为什么只投其中一个突然使整个陈述有效?任何见解将不胜感激!

最佳答案

10 在这里被当作文字 int 值,这个操作的结果是一个 int,编译器无法缩小范围。您应该显式添加类型转换:

s = (short) ((i < 100)? 1:0);

这在 Java Language Specification. Chapter 15. Expressions. 15.25. Conditional Operator ?. 中有解释

The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions.

(...)

If both the second and the third operand expressions are numeric expressions, the conditional expression is a numeric conditional expression.

(...)

Numeric conditional expressions are standalone expressions (§15.2).

The type of a numeric conditional expression is determined as follows:

  • If the second and third operands have the same type, then that is the type of the conditional expression.

  • If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.

  • If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.

  • If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression (§15.28) of type int whose value is representable in type T, then the type of the conditional expression is T.

  • If one of the operands is of type T, where T is Byte, Short, or Character, and the other operand is a constant expression of type int whose value is representable in the type U which is the result of applying unboxing conversion to T, then the type of the conditional expression is U.

  • Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.

Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

关于java - 无法在 Java 中使用 "(expr)?<val1>:<val2>"表示法设置短值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23684220/

25 4 0