简单面试题--值传递还是引用传递

package com.jo;

public class CTest {

private int id;

public void setId(int id) {
this.id = id;
}

static public void aa(CTest f) {
f = new CTest();
f.setId(3);
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CTest f = null;
aa(f);
System.out.println(f == null);
}

}

答案是 true

package com.jo;

public class CTest {

private int id;

public void setId(int id) {
this.id = id;
}

static public void aa(CTest f) {
// f = new CTest();
f.setId(3);
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CTest f = new CTest();
aa(f);
System.out.println(f.id == 3);
}

}

答案是 true

你可能感兴趣的:(面试,值传递,引用传递)