473,380 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,380 software developers and data experts.

Why String is immutable?

dmjpro
2,476 2GB
Can we have a discussion about it? ;)
Mar 30 '09 #1
12 9595
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

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

Similar topics

2
by: Fuzzyman | last post by:
I recently went through a bit of a headache trying to subclass string.... This is because the string is immutable and uses the mysterious __new__ method rather than __init__ to 'create' a string....
2
by: Tony | last post by:
tHi, I have application with memory leaks and I used .Net memory profiler. It showed that every time I use string, gc does not clear that string and at the end I have many instances and bytes...
6
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
18
by: greenflame | last post by:
I want to make a function that does the following. I will call it thefunc for short. '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>"
9
by: amyxmas | last post by:
I am trying to write a routine which follows this logic, but not sure how to implement: <script> xxx = "... apple green yellow blue ..."; {right here I need to modify part of the string, so...
30
by: nano2k | last post by:
Hi I need an efficient method to convert a string object to it's byte equivalent. I know there are LOTS of methods, but they lack in efficiency. All methods allocate new memory to create the byte...
6
by: DaTurk | last post by:
I'm converting MC++ to C++/CLI and for some reason it's not cool with static const System::String^ How am I supposed to have static constant strings then?
8
by: s0suk3 | last post by:
Hi everyone, I've heard that a 'str' object is immutable. But is there *any* way to modify a string's internal value? Thanks, Sebastian
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.