• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why only String class is immutable

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think there is no pool for ints or floats? There's actually info online about that.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shall we get back to the original subject before I move this thread to MD? Mwaahaahaahaahaahaahaa!
 
reply
    Bookmark Topic Watch Topic
  • New Topic