gpt4 book ai didi

java - 对角色进行一元运算后装箱

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:13 30 4
gpt4 key购买 nike

  public static void main(String[] args) {
Character x = ' ';
while (x++ < 50) {
int p = x;
}
System.out.println(x instanceof Character);
}

因为是一元运算++。 x 在执行操作之前被拆箱为 char,然后扩展为 int。当我退出循环时,我得到“真”。我不明白如何将 int 装箱到 Character 后面; java 可以允许将 int 装箱为 Integer 或将 int 装箱并扩大为 Number、Object 等。

最佳答案

答案就在 Java 语言规范中 §15.14.2 Postfix Increment Operator ++

(我强调)

At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored.

关于java - 对角色进行一元运算后装箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42633338/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com