java - Java中三元运算符中的char转换,打印int值而不是char值

标签 java casting conditional-operator primitive primitive-types

在 Java 中,您可以使用 char 值或 ASCII 值将 char 转换为 int,反之亦然。如果将 int 变量转换为 char,则会得到相应的字符。因此,以下代码片段将打印“a”:

int x = 97;
System.out.println( (char)x ); // 'a'

但是当我这样做时:

char ch = 'a', ch2 = 97, ch3 = 'b';
System.out.println( ( (ch+=1) > ch2 ) ? (char)ch2 : (int)ch3 );

编译器打印出 int 值 97,而不是“a”,即使“true 端”的三元运算符返回值是 (char)ch2。所以我期望的是 'a' 而不是 97。为什么它打印 97 而不是 'a'?

最佳答案

问题在于:

System.out.println(((ch += 1) > ch2) ? (char) ch2 : (int) ch3);

因为您有 (int) ch3,编译器假定三元运算符的返回类型将为 int

检查 JLS 15.25. Conditional Operator ? : 中规则的突出显示部分

The type of a conditional expression is determined as follows:

  • If the second and third operands have the same type (which may be the null 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 second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.

  • Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:

    • 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 (§15.28) 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).

  • Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2.

如果你这样做

System.out.println(((ch += 1) > ch2 ) ? (char) ch2 : (char) ch3);

它将打印

'a'

或者,您可以应用暴力破解,即将最终结果转换为 char,如下所示:

System.out.println((char) (((ch += 1) > ch2 ) ? (char) ch2 : (int) ch3));

它也会打印

 'a'

关于java - Java中三元运算符中的char转换,打印int值而不是char值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66615473/

相关文章:

java JPanel如何固定大小

c - 使用各种格式说明符打印 short int

arrays - typescript :为每个元素输入不同类型的数组

javascript - 条件运算符未正确评估

conditional-operator - 三元比较输出新值

java - Android 不接受根 CA 证书

java - Redshift - 选择查询表名称在双引号中不起作用

java - 在 JMeter 中为不同的线程使用不同的变量

c - 传递 `puts' 的 arg 1 使指针来自整数而不进行强制转换

c++ - 条件运算符无法解析重载的成员函数指针