0

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 app.setName("newName"), 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()

Community
  • 1
  • 1
Jonathan Andersson
  • 1,342
  • 3
  • 16
  • 31

2 Answers2

3

app.setName("newName") will create a new String "newName" and store it in the String pool, the String pointer in app is now replaced to point at the new String. The "name" String still exists.

The key thing to remember is Java string objects never change, if you create a new String, or modify an old one, it always results in the creation of a new object. Also, String is an object, so String variables always store references to a String stored elsewhere (in the heap with your other variables, or in the String constant pool).

dahui
  • 2,128
  • 2
  • 21
  • 40
2

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.

Luan Nico
  • 5,376
  • 2
  • 30
  • 60
  • *"All references to objects are pointers"* no, they are not, because **Java has no pointers**. You can for simplicity handle references like pointers but they aren't none. – Timothy Truckle Nov 24 '16 at 10:31
  • o'rly Java has no pointers ? then a reference linked to some data in memory is not a pointer ??? The only difference from the C/C++ native pointers , is that you cannot perform arithmetic operations in order to loop or change where the pointer looks – AntJavaDev Nov 24 '16 at 10:35
  • 1
    @TimothyTruckle java doesn't have manually managed pointers like C (thankfully), but the references it holds work just like pointers: they are a reference to a specific place in memory. So in `Person a = new Person(), b = a;`, both a and b "point" to the same location in memory, so changes made inside person a will reflect on b and vice-versa. – Luan Nico Nov 24 '16 at 10:40
  • 2
    http://stackoverflow.com/questions/7436581/what-is-the-difference-between-a-pointer-and-a-reference-variable-in-java – Tom Nov 24 '16 at 10:44
  • @AntJavaDev *"The only difference from the C/C++ native pointers , is that you cannot perform arithmetic operations"* references are updated if the garbage collector moves the objecs in memory, with pointers you can't do that eiher. – Timothy Truckle Nov 24 '16 at 10:48
  • @TimothyTruckle so the native implementation of Java's GC is not using any pointers at all or the `ArrayCopy` commands , sure bro – AntJavaDev Nov 24 '16 at 10:51
  • @AntJavaDev *"the native implementation of Java's GC is not using any pointers at all"* the *native implementation* is not part of the Java language. It's just something that happens under the hood. – Timothy Truckle Nov 24 '16 at 10:59
  • yeap but you pointed out that native pointers cannot move objects in memory ..... does the pointer knows about objects ..... sure bro :) – AntJavaDev Nov 24 '16 at 11:06
  • 1
    To find out whether a reference is a pointer, we need to know what the words "reference" and "pointer" mean. The meanings of those words can be different in different contexts. A "reference" in Java is different from a "reference" in C++, for example (the latter cannot be null). The Java spec does (to my knowledge) not define what a pointer is, but it does contain the following sentence: "The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object." So, yes, a (non-null) reference is a pointer (but it's not a C++ pointer). – Hoopje Nov 24 '16 at 11:39
  • "so there is always one instance per value" - if you mean that two `"asdf"` strings will always be the same object, then no, that's completely wrong. Not all strings are interned. (Also, the term is "interned", not "internalized".) – user2357112 Nov 25 '16 at 01:16