-5

I don't understand this code, why my string c don't changing in main method but changing in changeString. Can you explain me?

class MainClass {
    public static void main(String[] args) {
        String c = "lalala";
        changeString(c);
        System.out.println("str in main = "+c);
    }

    public static void changeString(String str) {
        str = str + "CHANGE!!!";
        System.out.println("str in changeString = "+str);
    }

}

Result:

str in changeString = lalalaCHANGE!!!
str in main = lalala
Boris Mitioglov
  • 1,092
  • 4
  • 16
  • 32
  • 6
    There are may be a thousand duplicate questions for this in Java. Check those on the related section on the right hand panel of this page – Rohit Jain Sep 05 '13 at 11:23
  • This link will explain you concept of string in Java [check this][1] [1]: http://javarevisited.blogspot.in/2010/10/why-string-is-immutable-in-java.html – yantrakaar Sep 05 '13 at 11:31
  • I was writing up an answer to this. You really should read http://python.net/~mwh/hacks/objectthink.html if you want to understand how variables work. Though the post is in Python, the concept is the same in languages that employ references to values. *TL;DR - variables are names bound to objects not buckets that hold objects* – D.Shawley Sep 05 '13 at 11:47

4 Answers4

4

Yes, the java string is immutable.

In changeString, you are passing in a reference to the string lalala and then you are changing the reference to one that points to lalalaCHANGE!!!. The original string object is not changed, and the reference in main still refers to that original object.

If you were to use a StringBuilder instead of a string, and append CHANGE!!! to that StringBuilder, then you would see the change reflected in viewing it at main:

class MainClass {
    public static void main(String[] args) {
        StringBuilder c = new StringBuilder("lalala");
        changeString(c);
        System.out.println("str in main = "+c.toString());
    }

    public static void changeString(StringBuilder str) {
        str.append("CHANGE!!!");
        System.out.println("str in changeString = "+str.toString);
    }

}

In this changed version you would get:

str in changeString = lalalaCHANGE!!!
str in main = lalalaCHANGE!!!
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • This link will explain you concept of string in Java [check this][1] [1]: http://javarevisited.blogspot.in/2010/10/why-string-is-immutable-in-java.html – yantrakaar Sep 05 '13 at 11:24
  • @yantrakaar Why are you commenting that to me? Comment directly to the OP perhaps? – nanofarad Sep 05 '13 at 11:25
3

Is Java String immutable?

String is immutable and it means that you cannot change the object itself, but you can change the reference ofcourse.

So when you do this in your changeString method:

str = str + "CHANGE!!!";

a new string memory object is created. But your c reference in main method is still pointing to the old sting memory object and hence prints lalala.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

String is immutable, of course:

Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.

http://docs.oracle.com/javase/tutorial/java/data/strings.html

str = str + "CHANGE!!!";

this code is returning a different String and replacing its reference into you variable, but the original String didn't change.

Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
0

Yes ,Java String is Immutable.

Strings are constant; their values cannot be changed after they are created.

And String in java has very special treatment

Read more here :


Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307