Editing Pointer
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 197: | Line 197: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | It is clear from this example that <code>f</code> and <code>g</code> are not value ''instances'' of the class <code>Foo</code>; if they were, then the statement <code>g = f;</code> would create a copy of <code>f</code>. Instead, <code>f</code> and <code>g</code> are like pointers with an anonymous referent constructed by the <code>new</code> invocation, so that after <code>f</code> is assigned to <code>g</code>, the two pointers will have the same referent. | + | It is clear from this example that <code>f</code> and <code>g</code> are not value ''instances'' of the class <code>Foo</code>; if they were, then the statement <code>g = f;</code> would create a copy of <code>f</code>. Instead, <code>f</code> and <code>g</code> are like pointers with an anonymous referent constructed by the <code>new</code> invocation, so that after <code>f</code> is assigned to <code>g</code>, the two pointers will have the same referent. On the other hand, in semantics, <code>f</code> and <code>g</code> are more like C++ references; it would not make sense to use <code>&</code> and <code>*</code> operators because these Java pointers do not exist independently of their referents; only one or the other is exposed in the language proper. (Primitive types such as <code>int</code> cannot be referred to with pointers, and they cannot be passed by reference.) |
When an object is passed into a function, it is as though a pointer to the object's data were passed into a C function. Hence, if a function takes a parameter <code>Foo f</code>, then assigning to <code>f</code> inside the function will simply rebind the name, and leave the original argument untouched; but any modification to <code>f</code>'s members will be reflected in the original argument, as it entails the dereferencing of this pointer. | When an object is passed into a function, it is as though a pointer to the object's data were passed into a C function. Hence, if a function takes a parameter <code>Foo f</code>, then assigning to <code>f</code> inside the function will simply rebind the name, and leave the original argument untouched; but any modification to <code>f</code>'s members will be reflected in the original argument, as it entails the dereferencing of this pointer. | ||
− | + | One can also argue about whether Java passes parameters by value or by reference. If we insist that Java passes all parameters by value, then we must accept that <code>f</code> and <code>g</code> have pointer nature, because if <code>f</code> is passed into a function, and the parameter that receives it is reassigned via the assignment operator, then <code>f</code> is unchanged, but if a method is called on the parameter that mutates a data member of <code>Foo</code>, then those changes will be reflected in <code>f</code>, which is just what we would expect if a C++ pointer were passed (and the method called with the <code>-></code> operator). However, the lack of independent existence of pointers and their referents is taken by some to imply that Java cannot really said to possess pointers. Thus, there is considerable controversy over the question of whether Java has pointers. | |
− | < | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
− | + | ||
− | < | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
− | + | ||
− | + | The names <code>f</code> and <code>g</code>, at any rate, probably do not refer to fixed memory addresses; this stands in contrast to C, where pointers probably ''do'' refer to fixed memory addresses (but this is not required by the standard). This is because Java's automatic memory management is allowed to move values around in memory, and "pointers" must therefore be defined in a way that allows them to follow this movement. | |
<!-- TODO: Talk about Lisp, Scheme, Haskell, ML, and Perl --> | <!-- TODO: Talk about Lisp, Scheme, Haskell, ML, and Perl --> | ||