我有两个例子我不明白
Java将值作为变量传递或通过引用传递
为什么在Ref
类中整数变量没有改变(null)
?为什么在RefCol
类中修改集合变量col(1)
?
类参考:
test(): entero: 5
inicio(): entero: null
类 RefCol:
test(): col: [1]
inicio(): col: [1]
.
import java.util.Collection;
import java.util.Vector;
public class Ref {
public static void main(String[] args){
Ref ref = new Ref();
ref.inicio();
}
public void inicio(){
Integer entero = null;
test(entero);
System.out.println("inicio(): entero: " + entero);
}
public void test(Integer entero){
entero = new Integer(5);
System.out.println("test(): entero: " + entero);
}
}
public class RefCol {
public static void main(String[] args){
RefCol ref = new RefCol();
ref.inicio();
}
public void inicio(){
Collection col = new Vector();
test(col);
System.out.println("inicio(): col: " + col);
}
public void test(Collection col){
col.add( new Integer(1) );
System.out.println("test(): col: " + col);
}
}