Java Integer and Double objects have no extra overhead?

I work with Java 1.3, so it does not support generics. I came up with a shadow job. I want to know if Integer and Double objects have unnecessary overhead; I guess I ask: do whole units make as much space as int? The same question for Double and double.

From what I know, an array of objects contains an array of 32-bit integers that actually store the address in the object in memory, regardless of the array. But when I explicitly create an array of primitives like I am here, the result will be bad. Since AFAIK, an array of primitives is actually an array of data, not pointers to data. Or am I relying too much on C ++?

So, if the Array in the DataPackage really contains pointers instead of primitives, I have to go. But if they themselves hold the primitives, access to the data will be a problem, since double is 64 bits of data, but the pointer to it 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 this data, I allocated an array of objects as an array of integers, then call the intValue () function: temp is an array of ints. I should also indicate that getData in the input simply returns a DataPackage.

 temp[0] = ((Integer[])input.getData("Wheels").getData())[0].intValue(); 

I can’t just run this code right now because it is for FRC Robot at my school and at school.

+3
java object double pointers integer
Dec 28 '11 at 9:16 a.m.
source share
5 answers

It is hard to fulfill your question, but I can say the following:

  • The array of primitives int , double , etc., as you suspect, is simply an array of values, it does not indicate values.
  • Integer will have more memory than an int , because an Integer object is an object, so you will have the overhead of the object where the int will occupy only the required data space. Same thing with double , etc.
+2
Dec 28 '11 at 21:24
source share

You think too much about C ++. If you are worried about overhead, here is an article about it .

To summarize the article, Integer has 12 bytes of overhead over a regular int ; this is most likely because the JVM will try to align the data. A Long , however, is the same size as Integer , proving that it is an alignment problem.

+1
Dec 28 '11 at
source share

I am only thinking, but I think that integers take up several times more space than just an int and a pointer. I heard that the object itself has some overhead, which increases with each method. I would try running some tests on your system.

0
Dec 28 '11 at 9:26 a.m.
source share

As in this white document, in the java world you do not need to worry about overhead, although Integer takes a little more than the int type in terms of memory allocation. http://java.sun.com/docs/white/langenv/Simple.doc1.html

0
Dec 28 '11 at 21:30
source share

No, int and Integer do not occupy the same space. Int is 4 bytes, Integer will be more than that (how much more can be measured, but it depends on whether you work in 32 or 64 bit mode). The total size is 16 bytes for Integer.

Not sure what you want to execute, but your array will contain references to objects if you use Integer, Double, etc., but the values ​​themselves if you use int double, etc. However, since Integer, Double, etc. are immutable, I don’t know what you will get, only having links to objects.

And what is your system that only supports Java 1.3? EOL was started on October 25, 2004 and ended on December 11, 2006, so it’s quite outdated.

0
Dec 28 2018-11-11T00:
source share



All Articles