是否首先评估数组索引或赋值? [英] Is the array index or the assigned value evaluated first?

查看:82
本文介绍了是否首先评估数组索引或赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下代码:

array[index] = someValue;

首先评估someValue还是index?

推荐答案

首先评估索引.请参阅 JLS第15.26.1节,在特别是:

The index is evaluated first. See JLS section 15.26.1, in particular:

15.26.1.简单分配运算符=

...

如果左侧操作数是数组访问表达式(第15.13节), 可能用一对或多对括号括起来,然后:

If the left-hand operand is an array access expression (§15.13), possibly enclosed in one or more pairs of parentheses, then:

  1. 首先,左侧操作数的数组引用子表达式 评估数组访问表达式.如果评估完成 突然之间,赋值表达式就突然完成了 相同的原因(左操作数数组的)索引子表达式 访问表达式)和右侧操作数不会被评估,并且不会 分配发生.

  1. First, the array reference subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the index subexpression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.

否则,左侧操作数数组的索引子表达式 访问表达式被评估.如果此评估突然完成, 然后由于相同的原因,赋值表达式突然完成 并且不评估右侧操作数,也不会发生赋值.

Otherwise, the index subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and 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.

TL; DR :顺序为 1 [2] = 3

这篇关于是否首先评估数组索引或赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆