class Base
{
public int i;
public Base(int a)
{
i=a;
}
}public class StringTest { 
  public static void changeStr(String str){ 
    str="welcome"; 
  } 
  public static void changeBase(Base b)
  {
   b.i=6;
  }
  
  public static void main(String[] args) { 
    String str="1234"; 
    Base base= new Base(3);
    changeBase(base);
    System.out.println(base.i);
    changeStr(str); 
    System.out.println(str); 
  } 
} 我以为我明白了,可惜还是没明白
changeBase按“对象引用”的规则修改了i的值,
可是changeStr却没按“对象引用”规则,依然是“1234”
为什么呢?

解决方案 »

  1.   

    String对象有点特别
    一但生成一个String对象
    你就永远无法改变它
      

  2.   

    第一个改变,传的是对象进去,“对象引用”的规则.
    JAVA里面不存在传地址,所以依然是“1234”
      

  3.   

    第一个传的是对象b 对象b是不能改变的 
    第二个传的是string string自然也不能直接改变
      

  4.   

    你这样改下你看看
      public static String changeStr(String str){ 
        return "welcome"; 
      } 
      public static void changeBase(Base b)
      {
      b.i=6;
      }
      
      public static void main(String[] args) { 
        String str="1234"; 
        Base base= new Base(3);
        changeBase(base);
        System.out.println(base.i);
        str = changeStr(str);  
        System.out.println(str); 
      } String是不变类,必须显示更改结果
      

  5.   

    楼上  我试引用  你返回值有什么用啊楼上的楼上:
    第一个传的是对象b 对象b是不能改变的             //可是b变了 
    第二个传的是string string自然也不能直接改变     //这句是根据上一句推导出来的,所以这句也不成立
      

  6.   

    String and 基本类型都是fianl型的!你可以看看引用传递和值传递部分的书
      

  7.   

    str = changeStr(str);语句的作用其实就相当于str = "welcome";直接把返回值赋给str,所以显示出来字符串改变了
      

  8.   

    我是这样理解的:改变的是str的副本
      

  9.   

    http://www.javaeye.com/topic/25483java是永远都是传值,没有传引用
      

  10.   

    1, Java uses "Pass by Value" in method argument passing.For primitive variables, the actual primitive value is stored.For reference variables, which refer to instances of classes, the object reference is stored. This object reference is also called "pointer to the actual Object instance" or "the way (or the path) to reach the actual Object instance". When a primitive variable is passed as an argument to a method, a separated copy of the variable value is passed to the method. This copy of the value can be changed within the called method, but the orginal primitive variable is not affected.When a reference variable is passed as an argument to a method, a separated copy of the varialbe value is passed to the method as well. Over here, this variable value is the object reference (or the way to reach the actual Object instance). It's NOT the actual Object instance. The called method knows this object reference (the way to reach the original Object instance), so it can reach the original Object instance. This is why the called method can change the original Object instance (only if the Object instance is mutable).Let's see the example given. Reference variable "base" is pointing a mutable "Base" instance. That's why the "changeBase" method changed the original "Base" instance.2, String class is final, which means String instances are immutable. If a String reference variable is passed as an argument to a method, the method can reach the original String instance, but it can NEVER change it.In the given example, String variable "str" is assigned with value "1234". When "str" is passed in to method "changeStr", a copy of this String reference varialbe is passed to method "changeStr". Let's call this copy with the name "strCopy". Now, both "str" and "strCopy" point to the same String instance (value "1234"). Inside method "changeStr", String reference variable "str" (which is actually "strCopy") is assigned with new value, which points to another String instance (value "welcome"). The original String reference variable is not affected. It still points to the String instance (value "1234").
      

  11.   

    不知道我说的对不对,大家来说下
    因为实际上传递的是引用的副本,所以的话,
        Base base= new Base(3);
        changeBase(base);
    base引用只改变了对象的一个属性,并没有改变对象,
        String str="1234";
        changeStr(str); 
    str对象的副本已经被重写指向了“welcome”这个string对象,但是实际上str还是没有被修改,所以打印出来的还是1234,如果在changeStr(str)函数里增加一条打印语句估计会打印出welcome
      

  12.   

    3, Let's see another example here. Let's reuse the code above.
    -----------------------------------
    class Base
    {
    public int i;
    public Base(int a)
    {
    i=a;
    }
    }public class StringTest { 
      public static void changeBase(Base b)
      {
          b = new Base(7);
          System.out.println("Inside: " + b.i);
      }
      
      public static void main(String[] args) { 
        Base base= new Base(3);
        System.out.println("Before: " + base.i);
        changeBase(base);
        System.out.println("After: " + base.i);
      } 

    -------------------------------------What's the output?
    A:
    Before: 3
    Inside: 7
    After: 7B:
    Before: 3
    Inside: 7
    After: 3The actual answer here is B.Inside method "changeBase", Base reference variable b is initially points to the same Base instance (whose i is 3) as "base". Then it is reassigned with a new value, which points to a new Base instance (whose i is 7). The original Base reference variable "base' is not touched, nor does the original Base instance (whose i is 3).
      

  13.   

    my suggestion:System.out.println("Inside: " + b);
    System.out.println("After: " + base);
      

  14.   

    Good suggestion.It will be much better if you take the initiative to explain what the output is and why it is so. :-D
      

  15.   

    String 就可以看成和一般的基础数据类型一样 ,是值传递的