Since someone suddenly started creating lots of objects and I’ve no idea who it is :P, it doesn’t seem to effect performance but still something fishy is going on. Maybe I’ll find a way through the eclipse debugger or something :L
Ahh, I found it. I made the constructor private and looked at the errors. My binarySearch algorithm was creating an object to compare with the list. Is this how it’s supposed to work?
public int getIndex(int z) {
Entity e = new Entity(new Vector(0,0,z));
int insertPos = Arrays.binarySearch(entities.toArray(new Entity[0]), e,
zComparator);
if (insertPos < 0)
insertPos ^= 0xFFFFFFFF;
return insertPos;
}
[EDIT]: I guess that’s how it’s supposed to work, I was just a bit surprised by the amount of objects that were created solely to run the binarySearch(…), well, I suppose it’s not a problem.
As has been explain by others, I want the index position the object would take in the list regardless if it’s actually found or not. (Arrays.binarySearch(…) returns an inverted value - 1 the object would take if it is not actually found.)
You should take a look at VisualVM, which lets you query the heap with OQL, a SQL-like language. You can take a snapshot with jmap (comes with the jdk) and get a “report” on the various objects in the heap using OQL.
It’s just the way binary integer representation works, which is how integers are represented on all modern computers, including the java language spec. http://en.wikipedia.org/wiki/Two’s_complement for all the gory details, but the gist is that negative numbers are represented like positive numbers in binary (I hope you know how that works) with the leftmost (sign) bit set to 1, all other bits inverted, with 1 added. That extra addition is what makes it two’s complement, which has all kinds of extra benefits including doubling the range over one’s complement and avoiding signed zero.
Yeah I know how integers are represented in binary. I wrote a couple examples and it’s quite cool how XOR by -1 has that behavior. Also, can’t you do the same thing by doing ~value?