I’m writing a program which creates a large number of large arrays to store data. All of this data has to held in RAM, so I’m avoiding objects and currently using shorts to save space. These shorts serve as ID numbers which can be put into a lookup class to get the corresponding object on demand. I have recently questioned whether I’ll need the whole 2 bytes of a short, and so I’m now wondering if there’s anyway to define the data type being stored in one place in my code so that I can change it easily without having to hunt down every cast, return type, etc. that is currently set to short.
If I were willing to use objects I could easily just do
class MySmallNumber extends Short{}
and change the parent class if necessary.
If this were C/C++, i could use
#define small short
for the effect I’m looking for.
I’m searching for a way to do something like this in java that won’t require storing 64-bit object references in my arrays. Any help is greatly appreciated. Right now I’m looking at a really messy IDE replace all in order to do this.
I would suggest factoring out all code that depends on the type of your ID values into a separate class. Let that class handle all the operations (including lookup) that depend on whether the ID values are
short,byte, or something else. You can pass individual values in and out asshortor evenintvalues, even if internally they are converted tobyte. (This is, for instance, howjava.io.DataOutputStream.writeByte(int)was written—it takes anintargument and treats it as abytevalue.)