Topic: 方法可以修改对象参数的状态,怎么理解?

  Print this page

1.方法可以修改对象参数的状态,怎么理解? Copy to clipboard
Posted by: 鱿鱼笨笨
Posted on: 2007-09-11 08:40

书中有这么一句话:方法可以修改对象参数的状态,我搞了半天也没有弄明白是什么意思 ,是怎么改变的。
还望高手能指教一下。

2.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: andy_wang_5
Posted on: 2007-09-11 10:48

private int age = 0;

public void setAge(int age) {
this.age = age;
}

age是对象参数。
setAge是方法,setAge可以修改age的值。

我不是高手,我是这样理解的。由不对的地方请指正。

3.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: 鱿鱼笨笨
Posted on: 2007-09-11 15:37

呵呵,上面的哥们,你好像是错了啊。
方法不能修改基本类型的参数,private int age=0,这里的age不是int型的吗。

4.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: 鱿鱼笨笨
Posted on: 2007-09-11 15:53

好好看看这句话吧,我已经悟出来了:
“方法不能改变基本类型的参数,但对于对象参数来说,情况则有所不同,方法得到对对象引用的一个COPY,原来的对象变量和这个COPY所指向的是同一个对象。”这样就达到了利用方法来改变对对象参数的状态的目的。

5.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: Cappuccino
Posted on: 2007-09-11 23:32

方法不能修改基本类型的参数,private int age=0,这里的age不是int型的吗。

关键原因是因为基本类型的类里面并没有提供可以修改其真正的值的method。你如果赋值的话实际上是指向另外一个引用了。

private int age = 0;

public void setAge(int age) {
this.age = age;
}

age是对象参数。
setAge是方法,setAge可以修改age的值。

我不是高手,我是这样理解的。由不对的地方请指正。


你那个例子看着不清楚,未免楼主误解,看下面的例子吧~~

class Test
{
  public static void main(String[] args)
  {
    StringBuffer sb = new StringBuffer("Hi, this is the init value of StringBuffer.");
    change(sb);
    System.out.println(sb);
  }
  
  static void change(StringBuffer sb)
  {
    sb.append("\nThis Content is changed!!!");
  }
}

6.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: 鱿鱼笨笨
Posted on: 2007-09-12 06:22

真是谢谢楼上的朋友了,本来我还在纳闷为什么方法不能修改基本类型的值呢,这下是明白了.

7.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: ch0707
Posted on: 2007-09-12 14:02

对象引用不变,改变对象内容。

8.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-09-12 18:29

Java里面没有pass-by-reference的这个概念,所有的方法传递都是pass-by-value.d对于基本类型因为是传值,所以没有改变原来内存的数据,对于对象参数,传递的是引用的copy,(注意,也是值),所以指向相同的内存数据。

9.Re:方法可以修改对象参数的状态,怎么理解? [Re: HenryShanley] Copy to clipboard
Posted by: Cappuccino
Posted on: 2007-09-12 21:46

HenryShanley wrote:
Java里面没有pass-by-reference的这个概念,所有的方法传递都是pass-by-value.d对于基本类型因为是传值,所以没有改变原来内存的数据,对于对象参数,传递的是引用的copy,(注意,也是值),所以指向相同的内存数据。


呵呵,最近在找工作,正好在某讨论面试题的帖子里面看到讨论这个问题。其实只是看字面意思怎么理解了,其实都是传的reference,你可以理解是reference的值,理解成指向原来的指针也勉强说得过去~~~ 知道是怎么回事就是了,真正问起来的时候照着书上说的按值传递就行了。

10.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: 鱿鱼笨笨
Posted on: 2007-09-13 06:48

嘿嘿,真是谢谢大家的帮助.

11.Re:方法可以修改对象参数的状态,怎么理解? [Re: Cappuccino] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-09-13 22:02

Cappuccino wrote:
呵呵,最近在找工作,正好在某讨论面试题的帖子里面看到讨论这个问题。其实只是看字面意思怎么理解了,其实都是传的reference,你可以理解是reference的值,理解成指向原来的指针也勉强说得过去~~~ 知道是怎么回事就是了,真正问起来的时候照着书上说的按值传递就行了。


Your understanding actually does not keep the term straight and consistent. And it is a common error made by Java language newcomers. Indeed, even senior engineers find it difficult.

Java does manipulate objects by reference, and all object variables are references. However, Java does *not* pass method arguments by reference; it passes them by value.

If you do think Java passes argument by reference, think about the following simple swap program. This is confusing because JAVA does not support *pointers*.


public void badSwap(Object var1, Object var2)
{
Object temp = var1;
var1 = var2;
var2 = temp;
}


Notice that when badSwap() returns, the variable passed as arguments will still hold their original values. It is simply not how pass-by-reference works, since Java passes object reference by value. To be more accurate, when object type is passed to a method, JAVA passes the reference by value just like passing the primitive type, this means the references passed to the method are actually copies of the original references. (Notice that again, not the reference itself, but the copy it is, and i.e. the value of the reference.!!)

So let's look at the example above again, since the references are copies, swaps actually swap the copies and not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. (If it is pass-by-reference, the original reference should be affected, hence the value swapped)

(Some more information about how C/C++ handles passing method parameters, you can ignore the following paragraph if you are not familiar with pointers)
In C/C++, if we pass a value to a method, then pass-by-value is used. In contrast, if we pass the variable address by using (& sign), then pass-by-reference approach is adopted. However, a programmer needs to explicitly defines a method declaration using (* sign). However, in JAVA, we cannot control pointers.

Regards,
Jiafan

12.Re:方法可以修改对象参数的状态,怎么理解? [Re: HenryShanley] Copy to clipboard
Posted by: Cappuccino
Posted on: 2007-09-14 04:07

HenryShanley wrote:
Your understanding actually does not keep the term straight and consistent. And it is a common error made by Java language newcomers. Indeed, even senior engineers find it difficult.

Java does manipulate objects by reference, and all object variables are references. However, Java does *not* pass method arguments by reference; it passes them by value.

If you do think Java passes argument by reference, think about the following simple swap program. This is confusing because JAVA does not support *pointers*.


public void badSwap(Object var1, Object var2)
{
Object temp = var1;
var1 = var2;
var2 = temp;
}


Notice that when badSwap() returns, the variable passed as arguments will still hold their original values. It is simply not how pass-by-reference works, since Java passes object reference by value. To be more accurate, when object type is passed to a method, JAVA passes the reference by value just like passing the primitive type, this means the references passed to the method are actually copies of the original references. (Notice that again, not the reference itself, but the copy it is, and i.e. the value of the reference.!!)

So let's look at the example above again, since the references are copies, swaps actually swap the copies and not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. (If it is pass-by-reference, the original reference should be affected, hence the value swapped)

(Some more information about how C/C++ handles passing method parameters, you can ignore the following paragraph if you are not familiar with pointers)
In C/C++, if we pass a value to a method, then pass-by-value is used. In contrast, if we pass the variable address by using (& sign), then pass-by-reference approach is adopted. However, a programmer needs to explicitly defines a method declaration using (* sign). However, in JAVA, we cannot control pointers.

Regards,
Jiafan


嗯,我明白你说的意思。我的意思也仅仅只是说具体怎么说要看当事人的文字理解了。指针的那个例子是我举例不当,确切的说是值传递和引用传递的文字上的争议而已。比方说有的人理解的是int、string一类的采用的值传递,对象是引用传递;而有的人理解的是都是引用传递,int、string只能传值是因为其没有能够改变自身值的成员变量。

13.Re:方法可以修改对象参数的状态,怎么理解? [Re: 鱿鱼笨笨] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-09-15 18:27

OK, but the terminology needs to be unique, not in an ambiguity way. As far as the object type is concerned, it passes a copy value of the original reference. Hence it is still pass-by-value, although we all know that it is the reference to the object we are operating. If JVM passes the original reference, then it is the real pass-by-reference like other languages do. Smile

ps, String is actually a object type not a primitive type.

Regards,
Jiafan


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923