PDA

View Full Version : quick and stupid java question



Tuna-Fish
March 12th, 2007, 12:26 AM
Is there any way to make a reference (pointer?) that points to a specific index in a int[] array?
It is trivial if the contents of that array are objects, but if they are primitives it seems impossible, is it really so?

phossal
March 12th, 2007, 12:39 AM
Like this?


package com;

public class Pointer {


public int[] x;


public Pointer() {
x = new int[3];
x[1] = 2;
}

public static void main(String[] args) {
Pointer p = new Pointer();

int p1 = p.x[1];
System.out.println(p1);

p.x[1] = 45;
System.out.println(p1);
}

}

Your goal is to end up with a var like p1 which reflects any changes in x[1] ?

Tuna-Fish
March 12th, 2007, 12:46 AM
That doesn't work, i tried.
the program prints
2
2
int p1 = p.x[1]; //Assigns the value of p.x[1] to p1, it doesn't make p1 a pointer to p.x[1]

Tuna-Fish
March 12th, 2007, 12:48 AM
Yes, the point is to have a var p1 which directly points to p.x[1].
p1 cannot be an int, just because int only ever stores the value, not a location.

phossal
March 12th, 2007, 12:58 AM
It would work something like this:


package com;

public class Pointer {


public int[] x;


public Pointer() {
x = new int[3];
x[1] = 2;
}

public static void main(String[] args) {
Pointer p = new Pointer();
System.out.println(p.x[1]);

Pointer q = p; //<--- THE POINTER
System.out.println(q.x[1]);


p.x[1] = 45;

System.out.println(q.x[1]);

}

}

phossal
March 12th, 2007, 01:09 AM
Removed

pmasiar
March 12th, 2007, 01:49 AM
Is there any way to make a reference (pointer?) that points to a specific index in a int[] array?
It is trivial if the contents of that array are objects, but if they are primitives it seems impossible, is it really so?

I am not a Java expert but IIUC values of primitive types are stored in stack frame, and are not available after exit (function's stack frame is out of scope). Java objects are stored in the heap, and they are "alive" (protected from garbage collector) as long as any reference to them is valid. This is difference between int (primitive) and Integer (object).