I am working with Java 1.3, so it does not support generics. I came up with a shady work around. I want to know if the Integer and Double objects have an unnecessary overhead; I guess what I am asking is: do Integers take up same amount of space as an int? Same question for Double and doubles.
From what I know, the array of Objects holds an array of 32 bit integers that actually hold the address to the Object in memory independent of the array. But when I explicitly make an array of primitives like I did here, the outcome would be bad. Because, AFAIK, an array of primitives actually are an array of the data, not the pointers to the data. Or am I just assuming too much from C++?
So, if the Array in DataPackage actually does hold pointers instead of the primitives, I am good to go. But if they hold the primitives themselves, accessing the data will be a problem since a double is 64 bits of data, but a pointer to that is still 32 bits.
/**
*
*
* @todo Comment all the code.
* @author Davidthefat
* @version 1.0
*/
public class DataPackage {
private String dataType;
private Object data;
/**
*
* @param type
* @param numOfItems
*/
public DataPackage(String type, int numOfItems) {
dataType = type;
if (type.equals("Wheels")) {
data = new int[numOfItems * 2];
}
if (type.equals("Arms")) {
data = new int[numOfItems * 1];
}
if (type.equals("Joysticks")) {
data = new double[numOfItems * 2];
}
if (type.equals("Buttons")) {
data = new boolean[numOfItems * 4];
}
}
/**
*
* @param t1
*/
public void update(Object t1) {
data = t1;
}
/**
*
* @return
*/
public Object getData() {
return data;
}
/**
*
* @return
*/
public String toString() {
return "This contains " + dataType;
}
}
To access that data, I cast the array of Objects as an array of Integers then call the intValue() function: temp is an array of ints. I must also put that getData in input just returns a DataPackage.
temp[0] = ((Integer[])input.getData("Wheels").getData())[0].intValue();
I can't just run this code right now since this is for the FRC Robot at my school, and school's out.
It's hard to follow your question but I can say this:
int
, double
etc is as you suspect, just an array of the values, it's not pointers to the values.Integer
will take more memory than an int
as an Integer
is an object, so you will have the object overhead, where an int
will take only the required space of the data. Same with Double
etc.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With