1

I have a string in method in java class as below

public void show(){
  String s1;
  s1 = "abc";
  s1 = "def";
  System.out.println(s1);
}

Here the output is def, but as String is immutable so I don't understand how string s1 is immutable here as I can change the String s1 content from abc to def.

Could you please make me understand this?

Arat Kumar rana
  • 187
  • 1
  • 5
  • 19
  • see the following link... it will clear your doubt. http://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning – Abhi Urs Jan 31 '15 at 12:52
  • Btw, if you want to prohibit the re-assignment of variables, then use the `final` keyword: `final String s1 = "abc";`. – Tom Jan 31 '15 at 12:59

4 Answers4

5

You did not change the string; you changed the variable s1 so that it refers to a different string. The original string "abc" still exists and has not been modified (and cannot be modified, because strings are immutable).

Try this:

public void show() {
    String s1 = "abc";
    String s2 = s1;
    System.out.println("s1 is same object is s2? = " + (s1 == s2));
    s1 = "def";
    System.out.println("s1 = " + s1);
    System.out.println("s2 = " + s2);
}

Notice that setting s1 to refer to a new string did not affect the original string, so s2 still refers to it.

davmac
  • 20,150
  • 1
  • 40
  • 68
  • I would say original string "abc" does not exist because GC took care of him. – kukis Jan 31 '15 at 12:45
  • @kukis I don't think that interned Strings (or String literals) will be cleaned up by the GC. (*Edit*: [but I might be wrong here](http://stackoverflow.com/questions/15324143/garbage-collection-of-string-literals)) – Tom Jan 31 '15 at 12:49
  • @kukis To be precise, here the `abc` will be eligible for GC only at the end of `show()`, whereas in the OP it was eligible for GC as soon as `s1 = "def"` was executed. – RealSkeptic Jan 31 '15 at 12:49
  • @kukis bringing garbage collection into this discussion is not helpful. In any case, at the point that the variable is being changed to refer to a different string, the original string _does_ still exist. – davmac Jan 31 '15 at 13:00
2

By mutable, you mean that you have the same object and you are able to modify its state (like say your class having a Date object). So immutable is exactly the opposite. Consider Date class and i define it like:

Date date = new Date();//print it and you will get today's date
date.setTime(date.getTime() - 10days in millis);//print and see you changed your date by bask 10 days which you changed internal state (field) of Date

Now in your example, you are reassigning a new String to your s1 String i.e. changing the reference to point from one string to the other. You are not changing the internal character array i.e. field of your String object.

SMA
  • 36,381
  • 8
  • 49
  • 73
2

String is immutable means that you cannot change the object itself, but you can change the reference to the object. When you called

  s1 = "abc";
  s1 = "def";

, you are actually changing the reference of s1 to a new object created by the String literal "def".

S1 is only the reference to the Object , it is not the actual object

String Constant Pool

Try this way

   s1="abc";
   s1.toUpperCase();
   System.out.println(s1);

Output will be still

   abc

Since s1.toUpperCase() instead of changing the contents of String s1 it creates a new Object with Upper Case Contents but since we haven't catch the returning variable that is why output is still same

Now try using

   s1="abc";
   s1=s1.toUpperCase();
   System.out.println(s1);

Now output

   ABC
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

If you try to create string object. The memory will be created on heap as well as in the string pool. For example: String str=new String("hello"); For the variable str memory will be created on heap with value "hello" as well as string "hello" will be created in the string pool. str is the reference variable which will refer to the "hello" in the string pool. Now if you try to create

String s1="hello";

Here s1 is the reference variable.String pool already has the value "hello". So s1 will refer to that "hello".

Same like this concept. s1 is the reference variable. It first refers the "abc" at String pool. then it refers to "def" at string pool. You didn't make any change on the "abc" or "def" at the string pool because they are immutable. If you want to create mutable string go for StringBuffer and StringBuilder.

Suganya Karthik
  • 131
  • 1
  • 1
  • 9