Talk is cheap,show me the code.
上代码:
import java.util.Arrays;
public class Test{
public static void main(String args[]){
int[] arr=new int[5];
int b=2;
arr[b]=b=3;
System.out.println(Arrays.toString(arr));
System.out.println(b);
}
}
这个最后输出结果是:
[0,0,3,0,0]
3
我想要问的是:连续赋值语句的执行的具体过程,为什么b=3 而不是给arr[3]赋值3?
请大神指教!
------解决思路----------------------
L2
LINENUMBER 6 L2
ALOAD 1
ILOAD 2
ICONST_3
DUP
ISTORE 2
IASTORE
字节码如上,执行过程中先将数组下标如栈,此时b=2。