package test;
import java.util.Enumeration;
import java.util.Hashtable;
public class TestVar {
public Integer count=0;
public MyVar obj=new MyVar();
public String changeParam(MyVar p_obj){
System.out.println("before:"+p_obj.toString());
// p_obj=new MyVar();//可以去掉此注释,运行一下会有不用的结果
System.out.println("after:"+p_obj.toString());
p_obj.ttt="222";
System.out.println("class MyVar instans:"+obj.ttt);
return p_obj.ttt;
}
public Integer changeParam(Integer p_count){
p_count =3;
return p_count;
}
public static void main(String[] args) {
Integer aa=1;
Integer bb=new Integer(1);
System.out.println(aa.equals(bb));
TestVar tv=new TestVar();
System.out.println(tv.changeParam(tv.obj));
System.out.println(tv.obj.ttt);
System.out.println(tv.changeParam(tv.count));
System.out.println(tv.count);
Hashtable table=new Hashtable();
StringBuffer sb=new StringBuffer("init str,");
table.put("1", sb);
sb.append("aaa,");
table.put("2", sb);
sb.append("bbb,");
table.put("3", sb);
sb.append("ccc,");
table.put("4", sb);
Enumeration aaddd = table.elements();
while (aaddd.hasMoreElements()) {
StringBuffer element = (StringBuffer) aaddd.nextElement();
System.out.println(element);
}
}
class MyVar{
public String ttt="ttt";
}
}
以下是输出结果:
true
before:test.TestVar$MyVar@5224ee
after:test.TestVar$MyVar@5224ee
class MyVar instans:222
222
222
3
0
init str,aaa,bbb,ccc,
init str,aaa,bbb,ccc,
init str,aaa,bbb,ccc,
init str,aaa,bbb,ccc,
结论:java中的方法参数,可以改变引用对象的属性,但是不能改变对象的引用。