Hi budy can you explain anyone which i have writen in the article title

Recommended Answers

All 5 Replies

String str = "asd";

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

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, 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?

Strings are handled differently.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.