原来也看过一些文章, 说基本类型用的是传值, 而对象用的是传址。 可是弄了半天还是不懂。 来看看我写的测试类:
class Objects {
public int value = 10;

public String toString(){
return String.valueOf(value);
}
}public class FunctionTest { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
FunctionTest test=new FunctionTest();
test.Test();
} public void changeValue(int i) {
i = i + 100;
println("In function, we change the value to " + i);
} public void changeValue(String s) {
s = s + 100;
println("in function, we change the value to " + s);
} public void changeValue(Objects o) {
o.value = 20;
println("in function, we change the value to " + o.value);
}

public void changeValue1(Objects o){
Objects obj=new Objects();
obj.value=2000;
o.value=9999;
o=obj;
println("in function, we change the value to " + o.value);
}

public void changeValue(int[] array){
if(array.length>0){
array[0]=9999;
println("in function we change the vlaue to 9999");
}
} public void print(Object o) {
System.out.print(o);
} public void println(Object o) {
print(o + "\n");
} public void Test() {
int i = 100;
println("before call the function, i is " + i);
changeValue(i);
println("after call the function, i is " + i);

String s="hello";
println("before call the function, s is " + s);
changeValue(s);
println("after call the function, s is " + s);

Objects o=new Objects();
println("before call the function, o is " + o);
changeValue(o);
println("after call the function, o is " + o);

Objects o1=new Objects();
println("before call the function, o1 is " + o1);
changeValue1(o1);
println("after call the function, o1 is " + o1);

int[] array={1,2,3,4,5};
println("before call the function, the array is:");
for(int n:array){
print(n+"  ");
}
print("\n");
changeValue(array);
println("after call the function, the array is:");
for(int n:array){
print(n+"  ");
}
print("\n");
}
}打印的结果(我把答案涂成白色了, 先想想答案再看):
before call the function, i is 100
In function, we change the value to 200
after call the function, i is 100
before call the function, s is hello
in function, we change the value to hello100
after call the function, s is hello
before call the function, o is 10
in function, we change the value to 20
after call the function, o is 20
before call the function, o1 is 10
in function, we change the value to 2000
after call the function, o1 is 9999
before call the function, the array is:
1  2  3  4  5  
in function we change the vlaue to 9999
after call the function, the array is:
9999  2  3  4  5  

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【hotleavegjz】截止到2008-07-01 10:13:31的历史汇总数据(不包括此帖):
    发帖数:0                  发帖分:0                  
    结贴数:0                  结贴分:0                  
    未结数:0                  未结分:0                  
    结贴率:-------------------结分率:-------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    那如何解释o1的现象? 在函数中对o1进行了赋值, o1=obj, 为什么返回的是9999? 而不是2000? 也是不10?
      

  3.   

    你尝试着把obj不要作为局部变量,作为全局变量去试试,看看还是不是这个结果,你自然会明白。
      

  4.   

    本帖最后由 AWUSOFT 于 2008-07-01 10:28:26 编辑
      

  5.   

    我说错了,sorry。
    刚才没注意到一个问题,现在解决了。
    o=obj这一步,想让o获得obj的值,并且在方法里好像也实现了。
    但是,请注意:o=obj这一步,改变了o这个变量的指向地址,但是并没有改变o这个变量之前所代表的对象的地址。
    不知道是否能看懂。
    而原来的o所指向的地址,其值仍然是9999,并不是2000。
    在test这个方法里,o所指向的地址仍然是9999所在地址,而不是2000。
    其实参数里的那个o,和外面的那个o,是不一样的。他们仅仅是一开始指向同一个地址而已。后面有了o=obj,两个就不一样了。
      

  6.   

    一开始:
    Test里的o----->10然后:
    Test里的o------>10<-----|
    ChangeValue里的参数o-----|执行了o.value=9999以后:
    Test里的o------>9999<-----|
    ChangeValue里的参数o------|
    ChangeValue里的obj-------->2000执行了o=obj以后:
    Test里的o------>9999
    ChangeValue里的参数o------>2000<----|
    ChangeValue里的obj-----------------|
      

  7.   

    Java中的方法调用中参数传递有两种,一个是对于参数是基本类型的使用的是值传递(直接传参数的值),另一个是引用传递,它是用于参数是类的对象,它传递的是这个对象的引用。
      

  8.   

    正好我这两天看了这个问题,你看看这个文章,应该对你有一定的帮助:http://blog.csdn.net/ec_group/archive/2008/07/01/2601122.aspx