How does String achieve immutability?

In an interview, they asked me: "How does String become immutable?" Since I was not sure of the answer, I did not answer. Later, I asked the interviewer about the same. The answer was the String class, which is final because immutability is achieved.

Is this the correct answer? if so, even StringBuffer is also marked as the final class. Then why is StringBuffer not immutable?

+4
source share
7 answers

This is a combination of:

  • Fields are private - so you cannot change them directly.
  • There are no set methods, so they also cannot be changed indirectly.
  • String is final - so you cannot add volatility to it (i.e. setters, etc.).
+11
source

No, this is not the right answer. String provides immutability because it does not provide you any way to change its internal contents. Thus, you can create an instance of the String object, assign a reference to it, but cannot change its contents after initialization.

+4
source

A string is an immutable object.

Make the class immutable by following these guidelines:

  • ensure that the class cannot be overridden
  • create final class or use static factories and keep private constructors
  • enter the private and final fields
  • they do not provide any methods that can somehow change the state of an object, not just setXXX methods, but any method that can change state if the class has any mutable fields of the object, then they must be protected from copying between the classes and his defiant
  • forcing calls to create an object completely in one step, instead of using a constructor with no arguments in combination with subsequent calls to setXXX methods (i.e., avoid Java Beans convention)
+3
source

The final keyword does not match immutability. String immutable since it does not define any methods that allow the user to modify its contents and , which is final, excluding the possibility of changing things in the subclass.

Creating something like the final variable of the List instance allows you to still modify its contents, making it mutable.

+2
source

Being final means that it cannot be received. It does not give immutability

Immutability is achieved by encapsulation and does not provide any means to fix the built-in array of characters. That is, there are no methods for changing internal fields.

+1
source

The string pool, initially empty, is privately maintained by the String class.

You should look at the JavaDoc strings: public native String intern();

Cm:

0
source

The usual way to make a class immutable is to ensure that:

  • all fields are private;
  • it is impossible to change the fields after construction;
  • it is final (so extensions cannot break immutability).

The line is a little special, at least in the Sun / Oracle implementation, since it does not actually perform this procedure. The implementation has a variable field in which it caches the hash code of the object. Therefore, although there is a method that changes the internal state of an object ( hashCode ), this state change does not change the behavior of the object. Any subsequent hashCode calls will work faster, but the result will not be different.

0
source

All Articles