Java code
public class My {    public static void main(String[] args){
        
        String str1="a";
        String[] str2={"a"};
        
        changes(str1);
        changes(str2);
        
        System.out.println(str1);
        System.out.println(str2[0]);
        
    }
    
    public static void changes(String a){
        a=new String("hello world");
    }
    
    public static void changes(String[] a){
        a[0]=new String("hello world");
    }
}打印出来的是: a             hello worldstr1为什么没有改变呢????

解决方案 »

  1.   

    这就是String 的不变性啊!str1还是str1他在内存中对应的值就是"a",
    另外new的a对应的才是:"hello world"!1.
    str1--->"a"
    a-->"hello world"
    2.
    str2是数组,他的数组中的第一个值将会变!
      

  2.   

    还是不懂哦...
      String str1="a"; 
      changes(str1); 
      public static void changes(String a){ 
            a=new String("hello world"); 
      }我给str1初始化后已经调用了changes()让str1改变它的值了.
    为什么就不变呢??
      

  3.   

    java中都是值传递,传递的都是值的拷贝,具体如下:
    String str1="a"; str1-->"a"(这个明白吧?不明白就没办法说下去了)changes(str1); 
    public static void changes(String a){
        a=new String("hello world");
    }
    a先-->"a" 然后 a-->"hello world" 但此时 str1仍-->"a"
    ------------------------------------------------------------------
    String[] str2={"a"};str2-->{"a"}changes(str1); 
    public static void changes(String a){
        a=new String("hello world");

    同上 a-->{"a"}(这个{"a"}还是上面那个,即此时a和str2同时-->{"a"})
    然后a[0]=new String("hello world") 即{"a"}被修改成{"hello world"}
    str2也-->{"hello world"}所以会有上面那种结果
      

  4.   

    public final class Stringextends Objectimplements Serializable, Comparable<String>, CharSequenceThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example: 
         String str = "abc";
     is equivalent to: 
         char data[] = {'a', 'b', 'c'};
         String str = new String(data);
     Here are some more examples of how strings can be used: 
         System.out.println("abc");
         String cde = "cde";
         System.out.println("abc" + cde);
         String c = "abc".substring(2,3);
         String d = cde.substring(1, 2);
     The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification. Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown. A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String. The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values). 
      

  5.   

    public class My { public static void main(String[] args) { String str1 = "a";
    String[] str2 = { "a" }; changes(str1);
    changes(str2); System.out.println(str1);
    System.out.println(str2[0]); }    //简单地说当参数是引用类型传进去后,引用的地址不会改变,但这地址里的内容可以改变
    public static void changes(String a) {
    a = new String("hello world");//此方法说明引用的地址不会改变
    } public static void changes(String[] a) {
    a[0] = new String("hello world");//此方法说明地址里的内容可以改变,a[0]引用是a的内容
    }
    }
      

  6.   

    String str1 = "a";
    //创建str1到"a"的引用,也就是"a"在内存中的地址
    changes(str1);
    //把str1的值拷贝给 a ,也就是a现在也是到"a"的引用
    //a = new String("hello world");
    //将 a 的值指向一个新的对象 "hello world"//所以str1的值是不会变的,依然是到"a"的引用,也就是"a"在内存中的地址    public static void changes(String a){ 
            a = new String("hello world"); 
        }
      

  7.   

    进入方法时(--- 上面是实参,下面是形参):  +--------+         +-----------------+
      |  str1  | ------> |       "a"       |
      +--------+    +--> +-----------------+
                    |
    -------------------------------------------
      +--------+    |
      |  str1  | ---+
      +--------+  +--------+         +-----------------+         +-----------------+
      |  str2  | ------> |     str2[0]     | ------> |       "a"       |
      +--------+    +--> +-----------------+         +-----------------+
                    |
    ----------------------------------------------------------------------
      +--------+    |
      |  str2  | ---+
      +--------+
    方法执行完后:str1 = "hello world";  +--------+         +-----------------+
      |  str1  | ------> |       "a"       |
      +--------+         +-----------------+
    ----------------------------------------------------------------------
      +--------+         +-----------------+
      |  str1  | ------> |  "hello world"  |
      +--------+         +-----------------+
    str2[0] = "hello world";  +--------+         +-----------------+         +-----------------+
      |  str2  | ------> |     str2[0]     | ------> |  "hello world"  |
      +--------+    +--> +-----------------+         +-----------------+
                    |
    ----------------------------------------------------------------------
      +--------+    |
      |  str2  | ---+
      +--------+
    退出方法后:  +--------+         +-----------------+
      |  str1  | ------> |       "a"       |
      +--------+         +-----------------+  +--------+         +-----------------+         +-----------------+
      |  str2  | ------> |     str2[0]     | ------> |  "hello world"  |
      +--------+         +-----------------+         +-----------------+
      

  8.   


    Java code 
    public class My {     public static void main(String[] args){ 
            
            String str1="a"; 
            String[] str2={"a"}; 
            
            changes(str1); //str1为实参
            changes(str2); //str2为实参        
            System.out.println(str1); 
            System.out.println(str2[0]); 
            
        } 
        
        public static void changes(String a){ //a为形参
            a=new String("hello world"); 
        } 
        
        public static void changes(String[] a){ //a为形参        a[0]=new String("hello world"); 
        } 
      

  9.   

    贴个代码,希望以后不要再有人看到类似的问题就想到String的不变性,一点关系都没有。public class Main {    public static void main(String[] args) {        StringBuilder str1 = new StringBuilder("a");
            StringBuilder[] str2 = {new StringBuilder("a")};        changes(str1);
            changes(str2);        System.out.println(str1);
            System.out.println(str2[0]);    }    public static void changes(StringBuilder a) {
            a = new StringBuilder("hello world");
        }    public static void changes(StringBuilder[] a) {
            a[0] = new StringBuilder("hello world");
        }
    }