• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why only String class is immutable  RSS feed

 
Bhairava Surya
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

It could be the simple question.

But I would like to understand why only String class is immutable. I looked into many forums, but not clear about it.

1. Why String class is immutable? What is the main reason for making String class as immutable.
2. Why there is no int pool or float pool or Integer pool etc, why only String pool


Thanks,
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you look at the bottom, you should see a list of "similar threads". This question has been discussed here many times. Read some of those, and see if they answer your question. If not, feel free to ask for clarification.



 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhairava Surya wrote:2. Why there is no ... Integer pool etc,


There is.
Well, an Integer cache, covering a couple of hundred values, usually -128 to at least 127.

And, since you mention Integer, it's also immutable.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think there is no pool for ints or floats? There's actually info online about that.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there is an Integer pool. It has a different name. Look here and in subsequent posts for a bit of discussion.
The reason for making Strings immutable is that immutability is a good thing.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, and that's why expressions like
String s = "im";
s += "mutable";
are impossible in java ;)

I found this a big hurdle to take when I started, many
moons ago.

Greetz,
Piet
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Indeed, and that's why expressions like
String s = "im";
s += "mutable";
are impossible in java ;)


Things seem to have changed in your favor, Piet. This works fine for me:


Output is this:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:hanged in your favor, Piet. This works fine for me:


But remember..that doesn't CHANGE the original String. It creates a brand new String object and re-assigned s to refer to it.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
But remember..that doesn't CHANGE the original String. It creates a brand new String object and re-assigned s to refer to it.


Very good point, fred. After Line 5 in Piet's code, s contains a reference to an immutable String. After Line 6, it also contains a reference to an immutable String, but not the same reference contained after Line 5. Here's some slightly(?) murky code that proves you're right:


And its output:
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes, it did not get the same attention as the lambda's, but nevertheless
a useful addition in java 8...
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Yes, it did not get the same attention as the lambda's, but nevertheless a useful addition in java 8...


Those danged FP proselytizers always hog the spotlights. Truth be known, if I'd had to choose between making "+=" work for String references, and that lambda tomfoolery... well...
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:[(...) danged FP proselytizers always hog the spotlights. ..

Wise words indeed... Floating Point neer brought us any good... <Big Smiley>
 
Bhairava Surya
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks one and all for your quick responses.

@All,

If we have Integer pool similar to String pool. Can someone explain how it works?

Also, I read the references shared here. After that also same question why only String class immutable. What are the specific advantages that we are getting with this.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhairava Surya wrote:I read the references shared here. After that also same question why only String class immutable. What are the specific advantages that we are getting with this.

I'm not sure what you really meant to say here but let me explain to you how this will be interpreted by most native English speakers. This looks like you are saying:

"I have read through the material that you referred me to but after reading them, I still have the same question: why is String the only immutable class? What are the specific advantages for making String immutable?"

Is this what you really meant? Because if it is, then you need to read the previous replies carefully: String is not the only class that is immutable and other people have given you examples of what those other classes are. For me, the biggest advantage of immutability, whether it's String or whatever, is that immutable objects are inherently thread safe.

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the usual example of how it works. first it prints abc and next it prints abcdef. the first one doesn't modify s. immutability prevents it. the second one doesn't modify it either but rather the assignment creates a whole new s which consists of old s concat ss . s += ss has the assignment built in of course, which is why that works.

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:

Yes, it did not get the same attention as the lambda's, but nevertheless
a useful addition in java 8...


If you're talking about += for Strings, that's been around for ages.
It's not a Java 8 thing.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shall we get back to the original subject before I move this thread to MD? Mwaahaahaahaahaahaahaa!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!