Quoting a paragraph from the JVM specification, see
for details
For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked. The new variable is initialized with the corresponding argument value from the method invocation.
The specification says that every time a method is called, the parameters in the method will be initialized.
The direction of test方法的index就是初始化了一个Integer类型的变量然后指向传入的i。后面的index = new Integer(20); 只是改变了index变量的指向,原来i above has not changed.
Quoting a paragraph from the JVM specification, see
for detailsThe specification says that every time a method is called, the parameters in the method will be initialized.
The direction of
test
方法的index
就是初始化了一个Integer
类型的变量然后指向传入的i
。后面的index = new Integer(20);
只是改变了index
变量的指向,原来i
above has not changed.Methods in Java all use value transfer. Even if it is a reference, what is passed is a copy of the original reference
The method can only change the attributes in the parameter reference, but not the reference address of the parameter
And you can see the source code of Integer:
The value it wraps is modified by final, that is, it can only be assigned once, and the value will not change after the assignment.
Also, you can try other reference types to see if they can be changed, such as String