1

I know Strings are immutable in nature. But I have a question.

String a = new String("abc");

If we create a string like above instead of a literal, then isn't it not immutable any more since it is created as a new object? Please clarify. Thanks.

Sildoreth
  • 1,883
  • 1
  • 25
  • 38
Ramson
  • 229
  • 4
  • 12

3 Answers3

6

No. It doesn't. A java String is always immutable irrespective of how it is created.

Actually using new to create Strings is redundant and should be avoided in 99 percent of the cases (unless you are doing some micro-bench marking)

Immutable means an instance cannot be modified once it is created. When you look at all the methods of String, none of them actually modifies the original String passed to it. They either return the same String or create a new one.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • But if we create two different string objects using `new` with same value (say "abc"), then both refer to two different memory locations which is nothing but acting as a normal objects. Then how can they be immutable. Thanks. – Ramson Mar 10 '15 at 13:42
  • @Ramson Strings are "immutable" because there is no way (no methods) to modify the data *inside* the String object that the pointer is pointing to. The fact that pointers are involved is inconsequential. – Sildoreth Mar 10 '15 at 13:44
  • 1
    @Ramson Strings are immutable but they do not enforce "uniqueness": you can create as many strings as you want that are equal to "abc". – assylias Mar 10 '15 at 13:44
  • 2
    @Ramson - *Immutability* means the value of an *instance* cannot be changed. *Immutability* is defined at *instance* level. Just because Strings are normal object s doesn't mean they should be mutable. *Wrapper classes are immutable*. I am not getting what you are actually trying to say here. :) – TheLostMind Mar 10 '15 at 13:45
  • Can you please tell me what is the main usage of String being immutable? Thanks. – Ramson Mar 10 '15 at 13:46
  • 1
    @Ramson :-) It is a design decision that was taken to improve performance. There are many low level optimizations that make use of the property of immutability. Simplest being, **removal of synchronization**. **Immutable instances are thread safe**. Also, String constants pool has strings which can be *reused* so less time spent on creation and more reuse. Lot of things.. – TheLostMind Mar 10 '15 at 13:49
2

Your String is immutable as String is always immutable (view The Java Language Specification)

The difference between String a = "abc"; and String a = new String("abc"); is not the mutability, is the use of the String pool. When you do

String a = "abc";

you are using the Java String pool (See String intern)

And when you do

String a = new String("abc");

you are not using the String pool.

The String pool stores different String instances, that's why when you create two different objects using the pool, the references are the same.

In both cases anyways, you cannot change the state of your String instance (String is immutable).

Creating Strings using new can be avoided almost always.

antonio
  • 18,044
  • 4
  • 45
  • 61
2

This is a mutable object:

class Person {
  private name;
  Person(String name) { this.name = name; }
  void setName(String name) { this.name = name; }
}

Because you can change its state after it has been created:

Person john = new Person("John");
john.setName("Fred"); //oops, John's name is not John any more...

On the other hand, once you have created a String there is no method that allows you to change the value of that string.

String john = "John";
john.setValue("Fred"); //No, that's not possible...

That does not prevent you from creating new Strings with similar or different values of course.

assylias
  • 321,522
  • 82
  • 660
  • 783