Jonathan Andersson Jonathan Andersson - 1 year ago 84
Java Question

Do Java create new String or pointer to orignal String?

I wonder if Java ever creates pointers to an other String.

One example where it could be a pointer:

App app = new App("name");
String name = app.getName();

If I after
String name
is initialized change the value with
String name
's value won't change.
This suggests that String name isn't a pointer but a new String.

But it could be, right? I mean it could be a pointer until either of the values (String name in App or String name) is changed to save memory?

So is it ever a pointer?

Not the same question as: String Interning
Which specifically asks for explanation of String Interning. e.g String.intern()

Answer Source

All references to objects are pointers; however, setName will change the value of the pointer, i.e., point it to somewhere else. Also, note that Strings are immutable and internalized, so there is always one instance per value and they never change.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download