Editing Pointer

Jump to: navigation, search

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.
  
Whereas in C we may have something like this:
+
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.
<syntaxhighlight lang="c">
+
typedef struct node
+
{
+
    int value;
+
    struct node* left;
+
    struct node* right;
+
} node;
+
</syntaxhighlight>
+
the equivalent code in Java would be:
+
<syntaxhighlight lang="java">
+
class Node
+
{
+
  public:
+
    int value;
+
    Node left;
+
    Node right;
+
}
+
</syntaxhighlight>
+
In the sense that pointers are necessary for the implementation of linked data structures, Java does have pointers; they are simply not presented as such to the programmer. The question is whether an entity can be called a pointer when its referent cannot be independently exposed in the language (in that it is impossible to pass an object "by value"). Also, there is no way to pass a primitive type such as <code>int</code> by reference, as would be done in C with a pointer to int; the int must instead be wrapped in an object. The inability of the programmer to create a reference to some data types may be a reason for saying that the language does not have pointers.
+
  
Of course, the knowledge of how to achieve equivalent effects to C's "pass by value" and "pass by reference" in Java and languages with similar reference semantics is far more important than a squabble over whether a language "has pointers" or not.
+
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 -->
  

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)