Valhalla Legends Forums Archive | Java Programming | imutable string!

AuthorMessageTime
touchstone
hi, while studying string , i found a phrase like
"String is imutable ".......i want to know what is the meaning of this ? any small example.?......is this true only for java ?
February 21, 2004, 8:07 PM
iago
It means a string can never be changed. Like this, for example:
String a = "abc";
a = a + "def";

the original String a is being destroyed, and a new stirng, "abcdef" is being created.

so if you do this:
for(int i = 0; i < 100; i++) a = a + "Number: " + i " + '\n';
it's actually doing A LOT of stuff:
each loop, it's creating a new string, which has number on the end and copies everything over. Then it creates a new string, with the number after it. Then it creates a new string that has '\n' at the end, then it loops again and does it all again.

No, not all languages are like this; Java is especially good at it, though, because it takes care of the cleanup so you don't leak memory when this happens.
February 21, 2004, 8:17 PM
Skywing
Note that iago's statement is usually false.

Most JVM programs I've seen have always seemed to only garbage collect when they run out of memory or reach a (large) preset size (like 30MB or so).
February 22, 2004, 5:33 PM
iago
[quote author=Skywing link=board=34;threadid=5391;start=0#msg45481 date=1077471183]
Note that iago's statement is usually false.

Most JVM programs I've seen have always seemed to only garbage collect when they run out of memory or reach a (large) preset size (like 30MB or so).
[/quote]

But it DOES clean itself up, at some point or other. If the same thing happens in C++, which never even TRIES to clean itself up, you would leak memory like mad.
February 22, 2004, 6:11 PM
Kp
Instead you only leak copious amounts of memory out into the void that is the JVM, rather than returning it to the OS for use by other programs. :) It's been my experience that anyone who willingly runs a Java application has accepted the inevitable that it will consume horrid amounts of memory, between of its tendency not to garbage collect effectively and its tendency to preallocate / post-gc-retain large amounts of memory.
February 22, 2004, 6:31 PM
Tuberload
[quote author=Kp link=board=34;threadid=5391;start=0#msg45490 date=1077474670]
Instead you only leak copious amounts of memory out into the void that is the JVM, rather than returning it to the OS for use by other programs. :) It's been my experience that anyone who willingly runs a Java application has accepted the inevitable that it will consume horrid amounts of memory, between of its tendency not to garbage collect effectively and its tendency to preallocate / post-gc-retain large amounts of memory.
[/quote]

You do have access to the garbage collector. So couldn't you make it work to your liking?

Edit: Grammar
February 22, 2004, 8:08 PM
iago
My friend was doing experiments with Java's GC once. It was on a Solaris machine, Java 1.3.1, and it would kick in at 30kb, I believe. I can find out the exact number tomorrow.
February 22, 2004, 8:49 PM
Kp
[quote author=Tuberload link=board=34;threadid=5391;start=0#msg45495 date=1077480538]You do have access to the garbage collector. So you couldn't you make it work to your liking?[/quote]

Quite honestly, I never tried. I didn't see it as being worth the time to clean up someone else's mistakes in designing a language I'm not yet required to use for anything serious.
February 22, 2004, 11:06 PM
Tuberload
[quote author=Kp link=board=34;threadid=5391;start=0#msg45518 date=1077491160]
[quote author=Tuberload link=board=34;threadid=5391;start=0#msg45495 date=1077480538]You do have access to the garbage collector. So you couldn't you make it work to your liking?[/quote]

Quite honestly, I never tried. I didn't see it as being worth the time to clean up someone else's mistakes in designing a language I'm not yet required to use for anything serious.
[/quote]

That makes sense. :) I will do it someday and post my results.
February 22, 2004, 11:16 PM

Search