Made a small test project in Java (forgive me for the horrible, horrible coding. I haven't done any Java in years. It took me five minutes to remember the 'public' keyword that has to preceed everything.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
// Point.java
package test;
public class Point {
int x;
int y;
Point(int X, int Y) {
x = X;
y = Y;
}
}
// Test. java
package test;
public class Test {
static public int add(int a, int b) { return a+b; }
static public int increase(int a) { a = a+1; return a; }
static public void increase(Point p) {
p.x = p.x+1;
p.y = p.y+1;
return;
}
public static void main(String[] args) {
int myA = 6;
int myB = increase(myA);
Point p = new Point(myA, myB);
increase(p);
System.out.print(p.x + p.y);
}
}
|
Theory:
If Java passes by value, then myA should be 6, myB 7, p (6, 7) and the output 13.
If Java passes by pointer, then myA should be 7, myB 7, p (8, 8) and the output 16.
If Java passes primitives by value and the rest by pointer, then myA should be 6, myB 7, p(7, 8) and the output 15.
Result: I get 15 as output.
Now, to test my memory (I messed up a piece of code I had to hand it because I didn't realize class copies were not deep copies), I added the line:
1 2
|
Point q = p;
increase(q);
|
The output changes to 17, confirming my memories of copy-by-reference/pointer.
[I don't know if this even remotely relates to what was being discussed, but it confirms what I remember; the way I understood rapidcoder's statement, this example disproves it.)