public class MethodDemo {
/**
* @param args
*/
public static void main(String[] args) {
MethodDemo demo = new MethodDemo();
Integer i = Integer.valueOf(1);
demo.add(i);
System.out.println("i:" + i);
String s = "ss";
demo.stringchange(s);
System.out.println("s:" + s);
Person per = new Person();
per.name = "per1";
demo.setDate(per);
System.out.println("per:" + per.getName());
}
//基本类型变不了
public void add(int i) {
i++;
}
/***
* 凡是在引用中出现修改引用的赋值语句,
* 修改都变成无效
* @param i
*/
//想修改引用,不行
public void add(Integer i) {
int j = i.intValue();
i = Integer.valueOf(j++);//i的原引用已经丢失了
}
//想修改引用,不行
public void stringchange(String s){
s = "stringchange";
}
public void setDate(Person per){
Person per2 = new Person();
per2.setName("per2Name");
per = per2;//per的原引用已经丢失了,这个估计很多人会出错
per.setName("name");
}
}
class Person {
String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}