I've got two examples I don't understand
Java passes the value as a variable or passes by reference
Why in the Ref
class the integer variable is not changed (null)
?
Why in the RefCol
class the collection variable is modified col(1)
?
class Ref:
test(): entero: 5
inicio(): entero: null
class 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);
}
}