469,271 Members | 1,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Why String is immutable?

dmjpro
2,476 2GB
Can we have a discussion about it? ;)
Mar 30 '09 #1
12 9200
r035198x
13,262 8TB
What really do you want to discuss about it?
Mar 30 '09 #2
dmjpro
2,476 2GB
@r035198x
Why String immutable ? i Just want to know the reason ?
Mar 30 '09 #3
r035198x
13,262 8TB
If they were mutable then there would be no String pool implementation and the performance gains of the String pool become lost.
Mar 30 '09 #4
JosAH
11,448 Expert 8TB
@dmjpro
Suppose they were mutable; would you've asked the opposite question?

kind regards,

Jos
Mar 30 '09 #5
dmjpro
2,476 2GB
No actually it seems to be odd. String simply character array. If want to change it then we need to have it another variable?
Mar 30 '09 #6
r035198x
13,262 8TB
It actually seems rather odd to me that you knowing that String is just a character array would expect changing it to be easy when you know all the restrictions of arrays ...
Mar 30 '09 #7
dmjpro
2,476 2GB
.........! I forgot that.
Then is the String is immutable for that reason?
Mar 30 '09 #8
r035198x
13,262 8TB
I did not say that is the reason. I was just using your words against you. I have already told you the performance reason in my first response.
Mar 30 '09 #9
JosAH
11,448 Expert 8TB
Note that this 'space saving' trick can turn its head against you; have a look at the following code snippet:

Expand|Select|Wrap|Line Numbers
  1. String humongous= ... ; // an extremely big String
  2. String small= humongous.substring(0, 1): // a very small String
  3. humongous= null; // free the big one?
  4.  
The substring() method shares its char[] buffer with the String for which it is a substring and the entire String buffer can not be garbage collected. Line two from the above code snippet had better written as:

Expand|Select|Wrap|Line Numbers
  1. String small= new String(humongous.substring(0, 1));
  2.  
The String constructor guarantees that a new char[] buffer will be used.

kind regards,

Jos
Mar 30 '09 #10
dmjpro
2,476 2GB
@r035198x
Actually sorry i didn't have a look at your first response.
@Josh
Thanks for your tricky code ;)
Mar 30 '09 #11
I hope below comments make every one in commendable position to answer " Why String is immutable".

Look at this example: We have a file open method with login check. We pass a String to this method to process authentication which is necessary before the call will be passed to OS. If String was mutable it was possible somehow to modify its content after the authentication check before OS gets request from program then it is possible to request any file. So if you have a right to open text file in user directory but then on the fly when somehow you manage to change the file name you can request to open "passwd" file or any other. Then a file can be modified and it will be possible to login directly to OS.


Note: I do agree with "String pool implementation and the performance gains of the String pool" but it all w.r.t VM rather than OS.

Cheers
Bunty
Aug 19 '09 #12
@JosAH

Expand|Select|Wrap|Line Numbers
  1. String humongous= ... ; // an extremely big String
  2. String small= humongous.substring(0, 1): // a very small String
  3. humongous= null; // free the big one?
  4.  
The substring() method shares its char[] buffer with the String for which it is a substring and the entire String buffer can not be garbage collected. Line two from the above code snippet had better written as:

Expand|Select|Wrap|Line Numbers
  1. String small= new String(humongous.substring(0, 1));
  2.  
This is not right. Call to substring is going to call the constructor String(originalString, startIndex, offset). So the substring is not sharing the char[] from original string
Feb 5 '12 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Fuzzyman | last post: by
90 posts views Thread by Ben Finney | last post: by
18 posts views Thread by greenflame | last post: by
9 posts views Thread by amyxmas | last post: by
30 posts views Thread by nano2k | last post: by
8 posts views Thread by s0suk3 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.