Forums Register Login

String classes

+Pie Number of slices to send: Send
String is immutable:

String s="hello";

s+="bfgkjdf";

Now the value of s is changed.. then how string is immutable?please explian it clearly
1
+Pie Number of slices to send: Send
You are creating a new String object"hellobfgkjdf" and substituting that for the original String. The two Strings "hello" and "bfgkjdf" still exist, but are unreachable because their references are changed.
2
+Pie Number of slices to send: Send
because s is not a string. it is a REFERENCE to a string. The String object "hello" still exists unchanged, but you no longer have a reference variable pointing to it.

It is similar to having an address card that points to a house. you can change the address written down on the card, but the original house is unchanged.
+Pie Number of slices to send: Send
niras iva,
Your post was moved to a new topic.
1
+Pie Number of slices to send: Send
 

I earlier wrote:niras iva,
Your post was moved to a new topic.

That was because you added an unrelated question to an old thread.
+Pie Number of slices to send: Send
You should know the String class was defined as public final class String.
+Pie Number of slices to send: Send
Welcome to the Ranch
What are your superhero powers? Go ahead and try them on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1222 times.
Similar Threads
Strings Immutable ??
regarding strings
Immutable classes
strings are immutable!!
regarding strings
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:18:38.