473,387 Members | 1,585 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,387 software developers and data experts.

Are runtime strings still imutable ?

Hi all,

Anyone know the details about String immutability ?

I understand the need to have imutable strings at compile time.
However if at runtime I have say 8000 strings and then I make
a new string from an existing string using substring
does that mean that my exisint 8000 strings will be searched
incase I am making a duplicate ?
That seems quite an overhead ?

Equally once I have created this new String once it becomes
eligible for garbage collection will the immutable String
be removed from the immutable string collection ?
IE will I just lose a reference to an immutable String object
or will that immutable String object be resuable for new
String objects that are created to have the same value ?
Does that mean creating/deleting lots of Strings can build
up an unfreeable area of memory ?

Thanks

Rob
Jul 17 '05 #1
4 3221
Rob Jones wrote:
Hi all,

Anyone know the details about String immutability ?

I understand the need to have imutable strings at compile time.
However if at runtime I have say 8000 strings and then I make
a new string from an existing string using substring
does that mean that my exisint 8000 strings will be searched
incase I am making a duplicate ?
That seems quite an overhead ?

Equally once I have created this new String once it becomes
eligible for garbage collection will the immutable String
be removed from the immutable string collection ?
IE will I just lose a reference to an immutable String object
or will that immutable String object be resuable for new
String objects that are created to have the same value ?
Does that mean creating/deleting lots of Strings can build
up an unfreeable area of memory ?


From what i know, strings are immutable yes. Once you don't use it
anymore it's elegible for garbage collection. There is a very good
reason why strings are immutable, it's more of a security concern.

Most of the time you don't have 8000 concurrent strings in memory, you
will read it from some sort of input stream. in that case then the gc
will be able to function no prob. its not as if you are doing String
myString7905 = "World";

if you do that and use it throughout the program ,then they will be in
memory for that scope.
Jul 17 '05 #2
Rob Jones wrote:
Hi all,

Anyone know the details about String immutability ?

I understand the need to have imutable strings at compile time.
However if at runtime I have say 8000 strings and then I make
a new string from an existing string using substring
does that mean that my exisint 8000 strings will be searched
incase I am making a duplicate ?
That seems quite an overhead ?


Even if there is such a check, it's almost certainly not linear.
References to strings in memory might be hashed, and any new string
might be sought in the hash before new memory is allocated. The
resources saved by not allocating/collecting more memory might warrant
the complexity of the hash. If you know otherwise, you can avoid the
immutability of Strings by using a StringBuffer.
Jul 17 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rob Jones wrote:
Hi all,

Anyone know the details about String immutability ?

I understand the need to have imutable strings at compile time.
However if at runtime I have say 8000 strings and then I make
a new string from an existing string using substring
does that mean that my exisint 8000 strings will be searched
incase I am making a duplicate ?
That seems quite an overhead ?

Equally once I have created this new String once it becomes
eligible for garbage collection will the immutable String
be removed from the immutable string collection ?
IE will I just lose a reference to an immutable String object
or will that immutable String object be resuable for new
String objects that are created to have the same value ?
Does that mean creating/deleting lots of Strings can build
up an unfreeable area of memory ?

Thanks

Rob


Hello,
This is not exactly what immutability means. Your example implies that
it is impossible to have two completely separate objects of type
java.lang.String, both of which happen to contain the same text. In
fact, this is very possible. What immutability really means is that,
given a certain java.lang.String object, it is impossible to change
the text inside it, as evidenced by the fact that methods like
substring() actually return a *new* java.lang.String containing the
substring, they *don't* modify the existing one.

What you were actually describing is the so-called "canonical
representation" mechanism, implemented by the intern() method of the
String class. This is completely unrelated to immutability with the
exception that the canonical representation mechanism could not exist
without the immutability property. Obviously, you never actually need
to call intern() unless you have a good reason.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD4DBQFAa2z2nwjA8LryK2IRAk/dAJdVa95zxPvSMUtVfHjon608RznlAJ45jvNh
rA8fkwLfXWeIJMyt/wac3Q==
=/N6h
-----END PGP SIGNATURE-----
Jul 17 '05 #4
"Rob Jones" wrote:
Anyone know the details about String immutability ?
See javadoc for String.intern()

It says:

"All literal strings and string-valued constant expressions are interned.
String literals are defined in §3.10.5 of the Java Language Specification "

You *can* call intern() for String with runtime values, but you don't need
to and would not normally do so.
I understand the need to have imutable strings at compile time.
However if at runtime I have say 8000 strings and then I make
a new string from an existing string using substring
does that mean that my exisint 8000 strings will be searched
incase I am making a duplicate ?
No only if you call intern(). It's a native method but it probably uses a
hash table.
That seems quite an overhead ?

Equally once I have created this new String once it becomes
eligible for garbage collection will the immutable String
be removed from the immutable string collection ?
IE will I just lose a reference to an immutable String object
or will that immutable String object be resuable for new
String objects that are created to have the same value ?
Does that mean creating/deleting lots of Strings can build
up an unfreeable area of memory ?


No but it creates a lot of garbage. Using StringBuffer for appending helps
there.
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bj?rn | last post by:
HY, my problem is the following: I want to give a very big application the ability, to change the language at runtime. It's written in Visual C++ 6.0. All language depending strings and so...
2
by: mike | last post by:
Hi People, I've found heaps of examples of how to localize C# apps from resource files at runtime, but the examples I've seen all seem to involve resources that are compiled in so that strings...
2
by: Steve Teeples | last post by:
How does one go about creating an enum at runtime from an array of strings? -- Steve
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
1
by: TeeCo | last post by:
Hi folks. I'm trying to change the location of the Access mdb file I connect to using OleDb and am having trouble. I'm using Visual C# 2005 and the default values I use for the ConnectionString...
2
by: Niyazi | last post by:
Hi everyone, I have 5 string in my array that I get from MS Access DB. Public mDov(0) as string and I fill with my for loop. The result as shown below mDov(0) = "ABC_01" mDov(1) = "ABC_02"...
3
by: samcogan | last post by:
Im writing an app that needs to let the user choose which SQL server they want to use froma drop down box, then connect to that server. I've been searching for ages to find a way to do this, anyone...
28
by: sturlamolden | last post by:
On Monday Microsoft announced a new runtime for dynamic languages, which they call "DLR". It sits on top of the conventional .NET runtime (CLR) and provides services for dynamically typed...
1
by: morrisqueto | last post by:
Hello, One of my websites just started sending a new rare error. The site has been working for almost 2 years without trouble, but today morning started giving away this error in all my views. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.