This is basically the same question as Implementing a C style bitfield in Java. But is there a way of doing this in a typed fashion without resorting to using a class? As an example, here is some 'C' code:
typedef struct
{
unsigned int x: 8;
unsigned int y: 8;
} point;
point getPoint()
{
point p;
p.x = 1;
p.y = 2;
return p;
}
"point" is nicely typed and the compiler passes it around as an int primitive type, which is very efficient. In java, one might use a class to contain the point and then write:
point getPoint()
{
return new point(1,2);
}
I am trying to improve a heavily recursive java game program that uses a class "point" (among other simple classes) and so the jvm is doing zillions of new operations and taking an enormous amount of time in garbage collection. Changing the class "point" into a packed int may or may not help, but its worth a shot. But I would like to have a nice type "point" to use in the program rather than just declaring "int" everywhere I use the point.