Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why String is Immutable ?

 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,


Can anybody explain why string is immutable ? or what are it's advantage ?


Thanks..

Regards,
Chetan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please SearchFirst

Why is String immutable?
 
Winston Gutkowski
Bartender
Pie
Posts: 10500
64
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chetan Dorle wrote:Can anybody explain why string is immutable ? or what are it's advantage ?

I'm afraid the answer is recursive: it's main advantage is that is it is immutable; and that, I suspect is why they made it so.

Just one advantage of immutable objects is that, in Java terms, they are Thread-safe; so you can pass a String around a multi-threaded environment with no worries.

Winston
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Just one advantage of immutable objects is that, in Java terms, they are Thread-safe; so you can pass a String around a multi-threaded environment with no worries.

Indeed
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indeed

i recently tried to pass an exception around. i quickly decided to pass a string instead.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:indeed

i recently tried to pass an exception around. i quickly decided to pass a string instead.


Color me confused.

Your code was modifying that Exception? In a multithreaded context? And you changed it to use a String instead?

Why not just not modify the Exception?

And it seems odd that your semantics are such that Exception and String are both appropriate to use in the same context.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this should clarify. this is from actionPerformed(). originally i passed a reference to the exception(e) to model. i changed it to pass the string.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:this should clarify


Not one bit. I don't do GUIs, and have no idea how Exception vs. String comes into play here.

Don't worry about it though.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i am not sure why but it didn't work when i passed the exception and it works now
anyway, i was just agreeing that being immutable isn't a bad thing
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:well, i am not sure why but it didn't work when i passed the exception and it works now


Depending on what you mean by "didn't work," it's highly unlikely that it has anything to do with mutability.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, this is way off topic but:
i had this comparison which i did in actionPerformed() and it entered the if. when i passed the exception to model and did the exact same comparison it did not execute the code in the if.



maybe i should just stay off the internet when i have been drinking
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:ok, this is way off topic but:
i had this comparison which i did in actionPerformed() and it entered the if. when i passed the exception to model and did the exact same comparison it did not execute the code in the if.




Still no idea what you're saying here, but I'm sure it has nothing to do with immutability. If you want to investigate it further, you can start a new thread or PM me, but I think we should let this tangent die in this thread now.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:If you want to investigate it further, you can start a new thread or PM me

Please here ...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Jeff Verdegan wrote:If you want to investigate it further, you can start a new thread or PM me

Please here ...


No. Let's not pollute this thread with any more off-topic discussion. Just start your own thread.
 
Winston Gutkowski
Bartender
Pie
Posts: 10500
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:No. Let's not pollute this thread with any more off-topic discussion. Just start your own thread.

Actually, I think what Seetharaman was saying is that PM's are best kept for truly private stuff (he is a Rancher; which I believe is a form of moderator). But I agree, t'other discussion maybe best in another thread.

Winston
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:he is a Rancher; which I believe is a form of moderator

Nope. It's a title administered just because one of the staffers felt like it.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:maybe i should just stay off the internet when i have been drinking

we are surrounded by bartenders!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49779
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote: . . . we are surrounded by bartenders!
But they may have suffered like the little man who used to grace the top of all JavaRanch pages
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

We are out of topic but anyways ....Thanks for your replies.


Hi Bear , Can you explain the hierarchy in the forum also ?










 
Campbell Ritchie
Sheriff
Pie
Posts: 49779
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bears outrank lions.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Bears outrank lions.


Packers outrank both.
 
Winston Gutkowski
Bartender
Pie
Posts: 10500
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Packers outrank both.

And Vikings'll burn the lot of yaz...and take your women.

Winston
 
Raj Kuppala
Greenhorn
Posts: 4
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Chetan Dorle wrote:Can anybody explain why string is immutable ? or what are it's advantage ?

I'm afraid the answer is recursive: it's main advantage is that is it is immutable; and that, I suspect is why they made it so.

Just one advantage of immutable objects is that, in Java terms, they are Thread-safe; so you can pass a String around a multi-threaded environment with no worries.

Winston


Hi Winston,
I have a doubt about that i think "Thread-safe" in the sense it is synchronized. When i wrote a program with String it is allowing multiple threads to change the content of it,but as it is thread-safe it should not allow to modify it.. But "String Buffer" is not allowing multiple threads at a time to modify it.. So now which one is thread-safe ? Its my doubt..


Which is thread-safe among String and StringBuffer
 
Winston Gutkowski
Bartender
Pie
Posts: 10500
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kuppala wrote:Hi Winston,
I have a doubt about that i think "Thread-safe" in the sense it is synchronized. When i wrote a program with String it is allowing multiple threads to change the content of it...

No it isn't. You can't, in the normal scheme of things, change a String; the only thing you can do is replace it with a different value. And if the value is passed as a parameter, any replacement will only affect the parameter itself, not the value it came from.

But, in answer to your last question (and depending on what your criteria are for "Thread-safe"): both, since StringBuffer is synchronized.
However, given the choice, I would always pass around a String rather than a StringBuffer, and I would generally use StringBuilder rather than StringBuffer.

Winston
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also note that StringBuffer being synchronized really only helps you in that multiple threads can modify the StringBuffer safely, however, the value of the StringBuffer can be modified out from underneath you in one thread in unexpected ways. For example, let's say you have this code:



You might expect it to be pretty save that the output should print "lo Th", the 5 characters starting at where "lo" was founds. But that is not a sure thing if another Thread is modifying the StringBuffer at the same time. For example, if another Thread runs:



If this code gets run at just the right time, between the indexOf() and substring() calls in the previous code, the output would be unexpected. To prevent this sort of thing you would have to add additional synchronization:


You do not have to worry about this sort of thing with String, since the String itself can't be changed - and therefore can't be changed out from under you in this way, and doesn't require additional synchronization.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kuppala wrote:
Winston Gutkowski wrote:
Chetan Dorle wrote:Can anybody explain why string is immutable ? or what are it's advantage ?

I'm afraid the answer is recursive: it's main advantage is that is it is immutable; and that, I suspect is why they made it so.

Just one advantage of immutable objects is that, in Java terms, they are Thread-safe; so you can pass a String around a multi-threaded environment with no worries.

Winston


Hi Winston,
I have a doubt about that i think "Thread-safe" in the sense it is synchronized.


Synchronization is not necessary to make an immutable class threadsafe. However, you can still use a threadsafe immutable class in a thread-unsafe manner.

When i wrote a program with String it is allowing multiple threads to change the content


No, it didn't. You cannot change the contents of a String because String is immutable, regardless of how many threads you have.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic