昨天在写一个小程序时,遇到一点问题,想了很久定位到是函数参数的问题上。
但是在把函数中的 int 类型换成 Integer 类型时,在我的函数体中,Integer类型涉及的操作只是自增,我发现最后读取的数值没有变化。
一度让我对Java函数参数的传递产生了怀疑。写了个test程序,总结了下,若是有什么错误,还望有人能够指出来。
import java.util.LinkedList;
import java.util.List;
public class TestFunctionCanShu {
public static void main(String[] args) {
TestFunctionCanShu question = new TestFunctionCanShu();
Integer cos = new Integer(0);
question.testIntegerAsCanShu(cos);
System.out.println(cos.intValue());
String cosString = "123";
question.testStringAsCanShu(cosString);
System.out.println(cosString);
StringBuilder sb = new StringBuilder("123");
question.testStringBuilderAsCanShu(sb);
System.out.println(sb);
StringBuffer sb2 = new StringBuffer("123");
question.testStringBufferAsCanShu(sb2);
System.out.println(sb2);
List<Object> list = new LinkedList<>();
question.testListAsCanShu(list);
System.out.println(list);
Node node = new Node(0);
question.testSelfClassAsCanShu(node);
System.out.println(node.value);
int[] array = new int[4];
question.testArrayAsCanShu(array);
System.out.println(array[array.length - 1]);
}
public void testIntegerAsCanShu(Integer i) {
/*
* 此处我认为可能是因为i++操作导致i被解封装导致i变为int类型
* 所以形参也指向为不同的值了
*/
i++;
}
public void testStringAsCanShu(String str) {
/* 因为String是不可变常量,
* 所以其实函数内复制引用(形参)指向的是另一段String常量,
* 而实参还是指向的是之前的String常量 */
str += "word";
}
public void testStringBuilderAsCanShu(StringBuilder sb) {
// sb = new StringBuilder("new String");
/*
* 形参和实参指向的是同一个对象,所以改变此对象有效
*/
sb.append("world");
}
public void testStringBufferAsCanShu(StringBuffer sb) {
/*
* 形参指向了另一个对象,对于实参来说没有影响
*/
sb = new StringBuffer("new String");
}
public void testListAsCanShu(List list) {
/*
* 形参和实参指向的是同一个对象,所以改变此对象有效
*/
list.add(new Object());
}
public void testSelfClassAsCanShu(Node node) {
/*
* 形参和实参指向的是同一个对象,所以改变此对象有效
*/
node.value = 4;
}
public void testArrayAsCanShu(int[] array) {
/*
* 形参和实参指向的是同一个对象,所以改变此对象有效
*/
for(int i = 0; i < array.length; i++)
array[i] = i;
}
}
class Node {
int value;
Node(int value) {
this.value = value;
}
}
输出结果是:
0
123
123world
123
[java.lang.Object@79a2e7]
4
3
我最大的疑惑大概就在于有没有什么办法,让传入参数是 Integer 类型,在函数对于其进行自增后,不使用return,而可以返回改变之后的对象。