Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Find out who is creating objects?  (Read 3023 times)
0 Members and 1 Guest are viewing this topic.
Offline jonjava
« Posted 2012-05-09 13:20:03 »

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

Offline jonjava
« Reply #1 - Posted 2012-05-09 13:24:00 »

Since someone suddenly started creating lots of objects and I've no idea who it is Tongue, 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

Offline jonjava
« Reply #2 - Posted 2012-05-09 13:30:16 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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.

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

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:

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


Quote from: Collections java doc
Returns:
    index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
[size=6pt]source: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#binarySearch%28java.util.List,%20java.lang.Object,%20java.util.Comparator%29[/size]

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-05-09 14:15:17 »

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

Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-05-09 14:27:25 »

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.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-05-09 14:33:07 »

What is the point of XOR-ing insertPos by -1 when it is less than 0?
1  
index^-1       ==      (index*-1)-1

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Roquen
« Reply #6 - Posted 2012-05-09 14:36:18 »

foo ^= (foo >> 31)
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-05-09 16:07:13 »

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.


 
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-05-09 16:08:09 »

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

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

Offline sproingie

JGO Kernel


Medals: 202



« Reply #9 - Posted 2012-05-09 16:23:37 »

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%27s_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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #10 - Posted 2012-05-09 16:44:47 »

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

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

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?

Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-05-09 22:17:21 »

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?

Offline ReBirth
« Reply #12 - Posted 2012-05-10 04:28:15 »

ra4king means this?
Quote
index = 127
index ^ -1 = index ^ 111... = index = 127 (ofc)
(index*-1)-1 = -index-1 = -(index+1) = -128 (dfq?)

Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2012-05-10 04:33:53 »

ra4king means this?
Quote
index = 127
index ^ -1 = index ^ 0111... = index = 127 (ofc) -128 (ofc)
(index*-1)-1 = -index-1 = -(index+1) = -128 (dfq?) -128 (ofc)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ReBirth
« Reply #14 - Posted 2012-05-10 04:53:12 »

ra4king means this?
Quote
index = 127
index ^ -1 = index ^ 0111... = index = 127 (ofc) -128 (ofc)
(index*-1)-1 = -index-1 = -(index+1) = -128 (dfq?) -128 (ofc)
why -1 == 0111...?
1 = 000...1, negating = 111..0, plus+1 = 111..1.

Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2012-05-10 04:56:40 »

127 in binary form = 01111111

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

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

Offline ReBirth
« Reply #16 - Posted 2012-05-10 05:01:57 »

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

Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #17 - Posted 2012-05-10 12:57:29 »

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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!