5
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

String str = "asd";

The String: "asd" is referred to by the object reference variable: str.

0

which basically means that "asd" is stored somewhere in your memory.
if you then perform:
str = "new asd";

this doesn't mean that the object "asd" doesn't exist anymore, but you don't have a reference to it anymore. now str will be a reference to "new asd" while "asd" remains on the same position in your memory, ready to either be referenced to again, or to be cleaned up.

0

stultuske, I thought once there is no reference to an object anymore, the object is destroyed... in that case, how would "asd" be referenced again? by recreating it? or am I missing something?

0

When your object has no remaining references it can be garbage collected. Because you have no remaining references there is no way ever to get it again (excluding very technical/obscure stuff with garbage collection internals).
As Norm says Strings are highly optimised in Java, so depending on how "asd" is created Java may keep that string in memory and re-use that memory later if you create another "asd" object - but this is strictly an internal optimisation and has no effect on how your program runs, other than maybe to make it faster.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.