I think Sinan's response hilights a number of issues and misunderstandings about Java and/or general programming.
Before getting bogged down in the details, I should make a statement about the notion that optimisation (for speed, code size, etc) should be one of the last things to consider when writing code. Especially in a learning-to-program sort of environment. I'm not (yet) a great Object-Oriented coder, but I do lament at seeing a lot of decidedly non-OO, awkward and inelegant code, especially when it seems to be through a lack of understanding and/or misguided instruction.
Sinan is the creator of the OpenProcessing site, and take that as experience enough to not be considered a newbie! However, there are some vague and also some incorrect notions about what is going on.
The OSC Media Controller code example defines a Button class, starting as follows:
Code:class Button {
// general variables used accross class
final int x = 0; final int y = 1; // variables to use with matrix size array
// ...
// matrix and cell related variables
float [] buttonSize = new float [2]; // width and height of each button
float [] buttonLoc = new float [2]; // location of the button
float [] relativeMouseLoc = new float [2]; // holds relative location of mouse on object (0,0 being top left hand corner)
// ...
// define the button class constructor
Button(int tXloc, int tYloc, int twidth, int theight) {
buttonLoc[x] = tXloc;
buttonLoc[y] = tYloc;
buttonSize[x] = twidth;
buttonSize[y] = theight;
}
My immediate reaction upon seeing "final int x = 0; final int y = 1;" was to think that was a bit strange. x and y are universally understood in many contexts to refer to some sort of position or vector, but here they are used as constant array indices.
Sinan suggests that "buttonLoc[0]" is fastest, and that "buttonLoc[x]" is "slower, since it is a dictionary". Firstly, since x is a constant ("final int"), I expect that it should be, or has the potential to be, exactly the same as using the literal constant 0 (by that I mean a compiler could produce the exact same executable code regardless of whether "x" or "0" are used, excusing cases where different variable types cause confusion). If x was not constant (that is, not "final"), then I agree there is another lookup required to find what the current value of x is each time, but I think Sinan is confusing the issue with thinking it is like using buttonLoc["x"] in PHP or something, where the "x" is not an integer variable but rather a dictionary key to look up in an associative array (which is, of course, far more compute intensive).
I also disagree that using buttonLoc[0] and buttonLoc[1] should be slower than using separate fields buttonLocX and buttonLocY; indeed I expect the latter to have
faster access.
Resoning:
buttonLoc[0] requires the location of the field (the buttonLoc array)
and an array index, which is not only a number, but (probably) a number that needs to be multiplied by another number (the size of an individual array element) to get the actual memory offset.
buttonLocX requires only the location of the field.
I should mention that, in both cases, the "location of the field" is itself an offset from the object's base location. How memory addresses are determined in Java is a somewhat unknown to me; my description here relates to the way things usually work for C or C++ structs and arrays.
I have tweaked someone else's Processing sketch that used an extra size-2 dimension on arrays for storing x and y values to instead use a separate x-values array and y-values array and noted a significant speed increase, so am fairly confident in saying that an unnecessary array indexing is not something you want to have when aiming for performance.
In C, structs inside structs aren't a problem at all. A sub-struct is as easy to access and use as a "top level" struct. I don't know if it can be done in Java (a quick look just now for "Java structs" yields the impression of some heated debate about allowing struct-like behaviour in Java), but I have a feeling that people using (array[0],array[1]) instead of (field.x, field.y) because they saw someone else do it, without understanding why and perhaps believing something that is the opposite of the intention.
-spxl