"Pointers" in java

Started by
4 comments, last by Raghar 18 years, 10 months ago
Hello, I'm writing a class in Java called VERTEX4D and I want it to be as similiar to a c-style struct as possible. I want the data for the vertex to be held in two ways: the first as x , y , z , w coordinates and the other in an array with four elements. However, I want to do this as simply as possible, and dont want to have to write several methods constantly setting the data equal to each other. I want x, for example, to be: COORDS4D [ 0 ] = &x , using C-style pointers, so when accessing this class both *COORDS4D [ 0 ] and x always equal the same thing. Obviously, the * and & operators do not exist in java, so I'm at a loss of what to do. Any help would be great, thanks.
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
Advertisement
You can't overload operators like [] in Java. The language philosophy is that you are not to be trusted with such things.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
I think you can't do operator overloading at all in Java. There are some built-in things that use it, but IIRC you can't do it yourself.
-Lord Maz-
Ok using your example.. You could do this a number of ways

//lets initialize first
ArrayList coords4DList = new ArrayList() // I typically use array list

or

VERTEX4D[] coordArray = new VERTEX4D[NUM_OF_COORDS]; // if you prefer an array


//so now we create your coord object from the class

VERTEX4D x = new VERTEXT4D();

// now we will add it to both the array list and the array

coords4DList.add(x);
coordArray[0] = x;

// at this point

(VERTEX4D) coords4DList.get(0) == x; // these point to the same object in memory
coordArray[0] == x; //these point to the same object in memory
(VERTEX4D) coords4DList.get(0) == coordArray[0]; // so obviously these point to the same object as well

When you use the '=' operator on an object you are passing by value a reference. They arn't the same as C++ references rather more like C++ pointers that are always dereferenced when you access them.
Oh, I forgot to mention. If you want the array to hold a reference to a different object in memory but same values as x at copy time, you will need to clone the object. You can have the class implement clone and then pass a clone instead.
vertex.a = 0
vertex.b = 2
vertex....

You get the idea

You are somewhat confuzed in capabilities of programming language and a usage of an other language. Just because in one language people (wrongly) abuses pointers (in a way that might be VERY bad on multiple CPU computers, or for compiler optimalizations.) it doesn't mean you should mindlessly copy all that syntax, and ignore the real algorithm.
You could easily access an public member of an object in Java. You should note that sometimes it could be faster to acces it throught get and set method and have it private. It depends on traps and JIT compiler optimalizations.

BTW I hope you don't like to do some aliasing. Dazzling of JIT compiler isn't recommended. Dazzling of users of a possible library isn't recommended either. Of course you could use ByteBuffer... at a highly likely speed penalty. (Actually not as high if you'd set native order, but you could forget inlining.)

Re ArrayList
I would very not recomend a class that have variable size as a base of class that has finite amount of members.

This topic is closed to new replies.

Advertisement