Patricia Shanahan wrote :
Wojtek said:
Patricia Shanahan wrote :
Yes, but the use of pointers is hidden from view. If you use pointers, then
you should be able to have an array of pointers. Which means that you
should be able to do pointer arithmetic.
A Java String[] is an array of pointers.
Pointer arithmetic, if available, can be used to construct array access.
However, that is rather like saying "A general GOTO can be used to build
structured statement organizations such as if-then-else."
A language can provide array access, including access to arrays of
pointers, without providing pointer arithmetic, just as easily as a
language can support if-then-else without having a general GOTO.
There is a difference between array access using indexes and array
access using pointers in the syntax. Yes, underneath the hood both
methods resolve to the same code, but the visible syntax is different,
and either method is used to illustrate the access method to ease
understanding of what is being done.
Or you can set a pointer to the beginning of the array, and then add 1
to it to move to the next element. In the code you are directly
accessing the second element.
I realize that in Java you can set a reference to the second element in
the array and then use that. But you cannot directly move that
reference to the third element. You can set it by using an index to the
thrid element.
I'm not at all sure what this means. It is the fact that a Java
reference "points" in the sense of identifying an object makes it a pointer.
How do you classify the parameter passing in the call to myMethod in the
following program? Call by reference? Call by value? Call by name? Or what?
I consider it to be call by value, where the parameter myBuf is a
pointer to the StringBuffer instantiated in main.
No it is a call by reference. If it was a call by value, then a clone
of the StringBuffer (in the following code) would be placed in the
stack. The method would then use the clone and the calling main() would
still use its private copy.
Types (objects) are passed by reference value. Primitives (int, long)
are passed by value.
Ok, let's take this apart.
1> public class CallByQuestion {
2> static StringBuffer x;
3> public static void main(String[] args) {
4> StringBuffer myBuf = new StringBuffer();
5> x = myBuf;
6> System.out.println("Before call:");
7> System.out.printf(" x==myBuf is %b%n",x==myBuf);
8> System.out.printf(" myBuf contents: %s%n", myBuf.toString());
9> myMethod(myBuf);
10> System.out.println("After call:");
11> System.out.printf(" x==myBuf is %b%n",x==myBuf);
12> System.out.printf(" myBuf contents: %s%n", myBuf.toString());
13> }
14>
15> private static void myMethod(StringBuffer myBuf) {
16> StringBuffer y = myBuf;
17> myBuf = new StringBuffer("Some data");
18> System.out.println("Inside myMethod:");
19> System.out.printf(" x==myBuf is %b%n",x==myBuf);
20> System.out.printf(" myBuf contents: %s%n", myBuf.toString());
21> y.append("Some other data");
22> }
23> }
More generally, how would you go about predicting and explaining the
output from the program?
2 - x is a static reference to a StringBuffer, set to null
4 - myBuf gets a reference to a StringBuffer(1) object
5 - x has a reference to the same StringBuffer(1) as myBuf
9 - myMethod is called passing in a reference to myBuf
15 - myMethod starts up with a reference to StringBuffer(1)
Note that the myBuf in the myMethod parameter is a different reference
variable than the myBuf in main(), and is in fact private to myMethod()
16 - y gets a reference to StringBuffer(1)
17 - myBuf now references a new StringBuffer(2)
21 - the StringBuffer(1) references by y has a String appended to it.
Since myBuf in myMethod is private to myMethod, then of course x==myBuf
in myMethod is false since they are referencing two different objects.