Reputation: 63
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.
Upvotes: 4
Views: 3054
Reputation: 11543
No, an int and an Integer does not take up the same amount of space. An int is 4 bytes, an Integer will be more than that (how much more can be measured, but it depends on if you are running in 32 or 64 bit mode). A common size seems to be 16 bytes for an Integer.
Not really sure what you want to accomplish, but your array will hold references to the objects if you use Integer, Double etc, but the values itself if you use int double etc. However, since Integer, Double etc are immutable I don't really know what you will gain by only having references to the objects.
And what system are you only that only supports Java 1.3? The EOL was initiated Oct 25, 2004 and ended December 11th, 2006, so it is quite aged.
Upvotes: 0
Reputation: 66637
As per this white paper, in java world you don't need to worry about overhead much, even though in terms of memory allocation Integer takes little higher than int type. http://java.sun.com/docs/white/langenv/Simple.doc1.html
Upvotes: 0
Reputation: 183
You are assuming too much from C++. If you are worried about overhead here is an article about it.
To summarize the article an Integer
has a 12 bytes overhead over a regular int
; this is most likely because the JVM will try to align data. A Long
is, however, the same size as an Integer
proving this is an alignment issue.
Upvotes: 1
Reputation: 1880
I'm only speculating but I imagine Integers take up a few times more space than just the int and pointer. I've heard the object itself has some overhead as well, which is increased with each method. I'd try running some tests on your system.
Upvotes: 0
Reputation: 19443
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.Upvotes: 2