基本类型传值,Object传引用

解决方案 »

  1.   

    照楼上的这么说,那就没有其他类型的传参方式了。赫赫
    C语言中,传递实参用的是地址,像你们说的,地址也是数值,也要在内存中占4个byte,所以也是传值????不太通吧,好像曲解了传参方式的意思。
      

  2.   

    呵呵,的确……有点,
    不过C++里的引用传递不是这样的呀,主要是JAVA的对象表示方法和C++不同,我倒是觉得这样比较好理解。
      

  3.   

    >>基本类型传值,对象传句柄,但是句柄本身是按值传递的。
    这句话是这个意思:由于句柄本身是按值传递的,所以即使在方法中改变了句柄的引用,使它指向了其他的对象,从方法返回后,它还是要指向原来的对象的。
      

  4.   

    例如这道题,考的就是这个:
    class ValHold{
    public int i = 10;
    }public class ObParm{
    public static void main(String argv[]){
    ObParm o = new ObParm();
    o.amethod();
    }
    public void amethod(){
    int i = 99;
    ValHold v = new ValHold();
    v.i=30;
    another(v,i);
    System.out.print( v.i );
    }

    public void another(ValHold v, int i){
    i=0;
    v.i = 20;
    ValHold vh = new ValHold();
    v =  vh;
    System.out.print(v.i);
    System.out.print(i);
    }
    }返回到amethod()后v.i是20而不是10,就是由于这个原因。
      

  5.   

    namowen是正确的。大家可以参考文章:http://www-900.ibm.com/developerWorks/cn/java/passbyval/index.shtml呵呵。
    我要散分了。
      

  6.   

    基本类型 + String对象
    其它对象传引用
      

  7.   

    基本类型 + String对象传值;
    其它对象传引用
      

  8.   

    不对 String也是用对象传递的,只不过jvm处理String是分了2部分
      1 String 对象,包括引用地址(指向2)
      2 ‘xxxx’在jvm的地址
     jvm会在启动时将所有字符串"ab"都统一分配地址
     所以当你用 a = b  = 'xxx' ;时  a和b都是指向XXX的地址
     即使对a 的值修改,也指挥生成新的字符串地址,不会影响其他的引用
      

  9.   

    in <thinking in java>
    -----------
    "Although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object", the auter give below reference:
    --------------------
    This can be a flashpoint. There are those who say “clearly, it’s a pointer,” but this presumes an underlying implementation. Also, Java references are much more akin to C++ references than pointers in their syntax. In the first edition of this book, I chose to invent a new term, “handle,” because C++ references and Java references have some important differences. I was coming out of C++ and did not want to confuse the C++ programmers whom I assumed would be the largest audience for Java. In the 2nd edition, I decided that “reference” was the more commonly used term, and that anyone changing from C++ would have a lot more to cope with than the terminology of references, so they might as well jump in with both feet. However, there are people who disagree even with the term “reference.” I read in one book where it was “completely wrong to say that Java supports pass by reference,” because Java object identifiers (according to that author) are actually “object references.” And (he goes on) everything is actually pass by value. So you’re not passing by reference, you’re “passing an object reference by value.” One could argue for the precision of such convoluted explanations, but I think my approach simplifies the understanding of the concept without hurting anything (well, the language lawyers may claim that I’m lying to you, but I’ll say that I’m providing an appropriate abstraction.)
      

  10.   


    In the first edition of this book, I chose to invent a new term, “handle,” because C++ references and Java references have some important differences.
    /**
    C++ references and Java references have some important differences.
    C++ references and Java references have some important differences.
    */