Say if i want a array of 4 int's
ex.
int[] iA = {32,33,34,35};
would this array take up more memory than declaring them as individual Int's?
ex.
int i0 = 32;
int i1 = 33;
int i2 = 34;
int i3 = 35;
Say if i want a array of 4 int's
ex.
int[] iA = {32,33,34,35};
would this array take up more memory than declaring them as individual Int's?
ex.
int i0 = 32;
int i1 = 33;
int i2 = 34;
int i3 = 35;
Each int is a primitive data type, whose sizes are defined in http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html.
The array is an object, which carries some overhead. There is a good discussion on how to find the size of Java objects in how calculate java array memory usage.
Space saving should not be the first concern influencing your decision here. You should think of answers to these questions:
int
s change in the future - if the answer is "yes", then you need an array.3
, do you need an ability to access i3
? If the answer is "yes", you definitely need an array.To answer your question, an array would take a few additional bytes of memory, because Java needs to allocate space to the array object itself. This overhead is noticeable when the number of int
s is small, but it quickly goes down as your array grows.
Individual int will take less memory as there is