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

String copy in same location in java

P: n/a
Hi

I have string in java now i want to replace that string but at same
time I want
this string is replaced on same memory location on which first string
was.
How I can do it. In simple replace it creates different memory
location for every string even in tha case of replace.

Thanks in advance
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Liz

"megha" <me***@4cplus.com> wrote in message
news:ee**************************@posting.google.c om...
Hi

I have string in java now i want to replace that string but at same
time I want
this string is replaced on same memory location on which first string
was.
How I can do it. In simple replace it creates different memory
location for every string even in tha case of replace.

Thanks in advance


It is not possible. Once a string is created, it cannot be
altered; if you try to alter it, you get a brand new string
instead with a different reference value. You can make a
copy of a string's reference.
Jul 17 '05 #2

P: n/a
megha wrote:
Hi

I have string in java now i want to replace that string but at same
time I want
this string is replaced on same memory location on which first string
was.
How I can do it. In simple replace it creates different memory
location for every string even in tha case of replace.

Thanks in advance


Strings are immutable in Java, so what you are asking cannot be done
with Strings.

However, StringBuffers are mutable and you may do such things with them.

HTH,
Ray
Jul 17 '05 #3

P: n/a
> I have string in java now i want to replace that string but at same
time I want
this string is replaced on same memory location on which first string
was.
How I can do it. In simple replace it creates different memory
location for every string even in tha case of replace.


Not sure what you mean by 'same memory location', it doesn't make much
sense for Java. Do you mean same reference?

i.e.

String str = "old stuff";
str = "new stuff";

This replaces the value of the string but retains the same reference.

- sarge
Jul 17 '05 #4

P: n/a
hi
you have mistaken it
strings are immutable in java
String str = "old stuff";
str = "new stuff";
This replaces the value of the string but retains the same reference.
- sarge


str="new stuff";
is equivalent to str=new String("new stuff");
thus this will also make str refer to new location in memory

regards
amey
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.