わからない例が2つあります
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);
}
}