String str = "asd";
The String: "asd" is referred to by the object reference variable: str.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
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.
stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 627
Skill Endorsements: 25
Strings are handled differently.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
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.
JamesCherrill
... trying to help
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33