XiaoneiInfoParser xiaoneiInfoParser = new XiaoneiInfoParser(doc);
XiaoneiInfo xiaonei = new XiaoneiInfo();
xiaoneiInfoParser.castToPojo(xiaonei);
System.out.println(xiaonei.getName());
突然发现xiaonei的值改变了,我一直以为JAVA都是值传递,吓我一跳
通过自己的一些测试,发现JAVA还有引用传递,对于基本类型是值传递,对于对象是引用传递
实践出真知,老看书,东西会忘,动手才能记得住
测试如下
import junit.framework.TestCase;
class A{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class SetA{
public void set(A a) {
a.setName("change!!");
}
public void swap(A a,A b) {
A temp=null;
temp=a;
a=b;
b=temp;
}
}
class PassValue{
static void exchange(int a, int b){//静态方法,交换a,b的值
int temp;
temp = a;
a = b;
b = temp;
}
}
public class TestJ2SE extends TestCase {
public void testPoint() throws Exception {
A a = new A();
System.out.println(a.getName());
SetA setA = new SetA();
setA.set(a);
System.out.println(a.getName());
A b = new A();
A c = new A();
b.setName("B");
c.setName("c");
System.out.println(b.getName()+c.getName());
setA.swap(b, c);
System.out.println(b.getName()+c.getName());
}
public void testJ2SE2() throws Exception {
int i = 10;
int j = 100;
System.out.println("before call: " + "i=" + i + "/t" + "j = " + j);//调用前
PassValue.exchange(i, j); //值传递,main方法只能调用静态方法
System.out.println("after call: " + "i=" + i + "/t" + "j = " + j);//调用后
}
}
看客莫笑我,我实乃小白一个