gpt4 book ai didi

java - java中变量的初始化和声明问题

转载 作者:行者123 更新时间:2023-11-30 04:42:12 27 4
gpt4 key购买 nike

当分配从右到左发生时,为什么以下不会给出编译器错误?

String str = str = "";

最佳答案

该行为由 Java Lanaguage Specification #15.1 定义关于赋值运算符。即(强调我的):

Otherwise, three steps are required:

  • First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.
  • Otherwise, the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
  • Otherwise, the value of the right-hand operand is converted to the type of the left-hand variable, is subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

因此表达式 str = str = ""; 的计算是从右到左,但左侧操作数 String str 首先被计算并创建变量。

关于java - java中变量的初始化和声明问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12004799/

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