你说的问题,我前天也在已解决问题中搜索了一回,最终也是搞得很糊涂,帮你UP一下,也关注。
.............
public static void main(String[] args)
  {
    StringBuffer strB1=new StringBuffer("aaaaaaaaa");
    StringBuffer strB2=new StringBuffer("bbbbbbbbb");
    swap(strB1,strB2);
    System.out.println(strB1);
  }
  public static void swap(StringBuffer strB1,StringBuffer strB2)
  {
    StringBuffer strBTemp;
    strBTemp=strB1;
    strB1=strB2;
    strB2=strBTemp;
  }
.............
用这个方法输出一定是aaaaaaaa
但是如果用下面这个方式,strB1便会改变,输出会是aaaaaaaOK。(搞不懂,希望多些人关注)
public static void swap(StringBuffer strB1,StringBuffer strB2)
  {
    strB1.append("OK!");
  }

解决方案 »

  1.   

    第一反应,可以。但要注意外部变量的访问级别。public是可以的,private是不能直接访问的。protect要看类的继承关系。
      

  2.   

    java是可以的,java的函数参数分为两种类型,一种是int ,long,String等基本变量,这种变量传入一个函数后,将不会改变原来的值.另外一种是Object,这种做为参数传入一个函数的话,如果改变了,原来的值也会随之而改变.
      

  3.   

    同意楼上的
    int等类型是传值调用
    Object类是传引用的如果需要改变int等基本类型变量的值
    可以把这些基本变量生成一个相应的包裹类来传
    如int对应的是Integer,你传的时候由相应的int值生成一个Integer类来传输就可以了
      

  4.   

    至于顶楼的问题,我想是因为第一个swap的strB1、strB2都是局部变量,它们之间引用的互换并不会作用回main,相反,第二个swap因为是直接在strB1后面添加,所以可以。关于传递,同意楼上的。
      

  5.   

    其实很多书上都已经说得很明白了,当然那些什么一月通,21天通的我就不知道了,那些书作者本身的水平就够令人怀疑的了。比如类A里面有public变量bpublic void change( A param ){位置一
        param = you value//在这里param和上面括号里面(位置一)的param已经不是同一个了。这个param是一个类似param_1得咚咚,是虚拟机自动转换的,你改变不了你传进来的那个值。所以java号称是传值的,java里面是没有传引用这种说法的。
        但是不管是位置一的param还是方法里面的这个param(相当于位置一param副本),他们指向的都是同一块内存空间,所以下面的语句是可行的
        param.b = you value//这里b的值是可以被你改变的
    }你直接改变值和用append的区别就在这里,2楼的那个兄弟可以仔细想想。
      

  6.   

    bluesmile979(笑着) 把我说得晕晕的,收藏仔细研究。
      

  7.   

    in:
    public static void swap(StringBuffer strB1,StringBuffer strB2)
      {
        StringBuffer strBTemp;
        strBTemp=strB1;
        strB1=strB2;
        strB2=strBTemp;
      }
    just the object reference pass to the swap,like a point in c and c++,
    the strB1 and strB2 only the point point to the object in heap, so,if 
    you type strB2=strB1,just to say the two point refer to the some place
    in heap,so the output of the program is aaaaaaaa
    and the second swap use a object reference pass to a submethod, 
    and use the method of that reference,it is just the oop thinking.so
    that is right,cool.
      

  8.   


     既然java不像c++那样可以传引用, 我觉得传引用是非常好的一项技术,从这点上,java好像输给了c++, 不知道java在这方面有哪些弥补呢?!
      

  9.   

    package your param 
    if you want to change it(String a String b)public class Pack 
    { String a;
      String b;
    }chage(Pack pack){
    pack.a="???";
    pack.b="!!!!";
    }it's ok