Strings of Same Value in Java?

Viewed 12848

a quick and confusing question. If Class A and Class B have this inside them:-

String name="SomeName"; 

and both classes are instantiated, is it true that both instances refer to same memory location of variable "name" say when we do this objA.name or objB.name ? which has value "SomeName" and since String is immutable, several instances of both classes of same JVM use the same variable repeatedly? I read somewhere online that, unless there is

String example=new String("something"); 

is used, the former declaration always creates one copy and it is used until all its applications are terminated for reclaiming memory. Note: I see several answers, which one do I count on, can someone conclude. Thank you all for your effort, appreciate it.

5 Answers
Related