By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Are runtime strings still imutable ?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
-----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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.