Storing a reference of an object vs using a 4 byte int ID

Hello suppose I have an object say: Equip which contains a bunch of variables in the class.
I have already created the object and stored it into a Library object so it can be fetched later by an int ID.

Now suppose I create a Character object that wants to wear the Equip. The character object has a field say equipWeared.
I can either make equipWear an int to store the ID of the Equip I made or make it Equip type which is the class Equip is.
I am not making a new instance of Equip but just referencing it by assigning the variable the Equip from the Library object.

Which would be better and more efficient? Should I just put in an int ID number or put in a reference to the Equip?

reference. Make the reference also have a method getID(); which returns the ID.
Otherwise each time you want to use this object you must get it from the library using the ID. This involves searching = slow. (I assume the library has a hashmap from Integer to Equip? Also It’s just a reference, it’s not a new object. Therefore it doesn’t take much memory or anything if that’s what you are worried about
By holding a reference, you only need to search for it once (when you get the Equip)

There’s no need to work around using references for performance reasons. References are guaranteed to be fast, while searching like Roland said will always be at best almost as fast. Even if your Equip instances are all in an array and you just write equipArray[equipWearID] to refer to it, I think references will be slightly faster. Besides, you also need to have a reference to a HashMap, some kind of List or an array containing the actual instances of Equips, right? This would make you need both an ID int AND a reference to the collection of Equips to pick from. So you’re actually asking if one reference is slower than one reference and an ID.

This kind of micro-optimization is ludicrous. Are you equipping millions of times a second? No? Then don’t worry about it. Use what is simpler and easier to maintain, and that’s going to be the direct reference (and in this case it happens to be faster). Storing and following an indirect reference like an ID should be the job of a database persistence layer if you’re using one, and not something you deal with by hand.

Yeah I agree with sproingie, you are getting to a level of optimization that is absolutely ridiculous:

Now got make a game! ;D