0

I'm sorry if this is an obvious question as I am a beginner in Java.

String objects are immutable, meaning that they can't be changed. So if you instantiate:

String s = "hello";

and then change the contents of s,

s = "goodbye";

does this mean that a new String object is created, also called s? Is the value of the original variable s being changed? Or is its reference being changed? Thank you in advance as the concept of string immutability is confusing me.

user8913
  • 53
  • 1
  • 4
  • 1
    It's reference is changed to a different string object, yes. And adding two string literals involves a third string result – Rogue Dec 25 '19 at 17:00
  • 1
    ```String s1 = "java"; s1.concat(" rules"); ``` do this. And Try ```System.out.println(s1)``` – papaya Dec 25 '19 at 17:01
  • 1
    Does this answer your question? [Is a Java string really immutable?](https://stackoverflow.com/questions/20945049/is-a-java-string-really-immutable) – Torben Dec 25 '19 at 17:05
  • 2
    You're confusing variables and objects. Variables have a name. Objects don't. When you do `s = "hello"`, you assign a reference to the String object "hello" to the variables named `s`. When you do `s = "goodbye"`, you assign a reference to *another* String object "hello" to the same variable `s`. So `s` referenced the object "hello", and now references the object "goodbye". No object is ever modified in that code. You just assign two different objects to a variable. – JB Nizet Dec 25 '19 at 17:07

2 Answers2

1

When you create a

String s = "hello";

after lookin in string pool, java creates a new string in string pool and returns a reference of that string to s.

Now when you change the contents of s as

s = "goodbye"; 

again if the goodbye is not in string pool, it will create a new string in string pool and returns a reference of newly created string.

So the answer your question is strings are immutable.

If you have many operations to perform and your string content is going to be changed frequently, you can use string builder or string buffer, these classes are mutable.

skorade
  • 36
  • 4
0

You interchange immutability with what final modifier can achieve.

If you declare a variable as final, it means the reference cannot point to another instance than initialized:

final String string = "A";
string = "B";              // won't compile

String anotherString = "A"
anotherString = "B";       // this is okey

The point of immutability is that the inner fields of the instance doesn't change. The methods of String returns a new String from the original one while it remains unchanged:

String string = "A";
string.toLowerCase();  // will be still "A"

String anotherString = "A";
String newString = anotherString.toLowerCase();

// anotherString is "A"
// newString is "a"

Note the use of immutable object doesn't require final modifier, yet it's used to achieve immutability itself in the object which is designed as immutable. This class is immutable since bar is instaninated once and never changed:

public class Foo {

    private final int bar;

    public Foo(int bar) {
        this.bar = bar;
    }
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183