aspose file tools*
The moose likes Java in General and the fly likes Why String is Immutable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why String is Immutable ?" Watch "Why String is Immutable ?" New topic
Author

Why String is Immutable ?

Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi All ,


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


Thanks..

Regards,
Chetan
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please SearchFirst

Why is String immutable?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

indeed

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


SCJP
Visit my download page
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 28, 2008
Posts: 5575

Jeff Verdegan wrote:If you want to investigate it further, you can start a new thread or PM me

Please here ...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Jan 10, 2002
Posts: 61770
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Randall Twede wrote:maybe i should just stay off the internet when i have been drinking

we are surrounded by bartenders!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
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

Joined: Aug 06, 2009
Posts: 146

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

Joined: Oct 13, 2005
Posts: 40061
    
  28
Bears outrank lions.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:Bears outrank lions.


Packers outrank both.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Jeff Verdegan wrote:Packers outrank both.

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

Winston
Raj Kuppala
Greenhorn

Joined: Nov 20, 2011
Posts: 4

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


Regards,
RP Royal.K,
Cognizant (India),
Mail Me: raj.prasad1224@gmail.com
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why String is Immutable ?