Find out who is creating objects?

Hi, is there a way in java to find out who is creating specific objects?

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.)

Ie instead of:

if (insertPos < 0)
   insertPos = -insertPos - 1;

source: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20java.lang.Object,%20java.util.Comparator)

What is the point of XOR-ing insertPos by -1 when it is less than 0?

Xoring by -1 means Inverting. Doing that, when the index is under 0, will make sure, the returned position is always positive.

index^-1       ==      (index*-1)-1

The javadoc of Arrays.binarySearch(…) explains it.

foo ^= (foo >> 31)

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.

:o I know about negating and subtracting one but wtf…XOR by -1 does the same?! me tries to figure out how this is true

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.

What sproingie said ( I had to copy paste your name to get it right :emo: ).

You’re familiar with that a bytes min and max value is [-128, 127], right? See that extra -1?

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?

ra4king means this?

why -1 == 0111…?
1 = 000…1, negating = 111…0, plus+1 = 111…1.

127 in binary form = 01111111

index ^ -1 = 01111111 ^ 11111111 = 10000000 = -128

index*-1 - 1 = 01111111 * -1 = 10000001 minus 1 = 10000000 = -128

And I thought ^ is AND which is confused in District Math symbol ~_~ I got it.

As ra4king suggested, why don’t they just use the NOT (~) unary operator? ???