wood burning stoves 2.0*
The moose likes Java in General and the fly likes int array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "int array question" Watch "int array question" New topic
Author

int array question

Rojer Smith
Greenhorn

Joined: Jun 21, 2003
Posts: 3
I am trying to use a piece of java code (not written by me), which has following declaration :
private int[] _q
....
_q = new int[2048];
Now, i am planning to increase the size of the int array to something like (to solve another problem...huh..) :
_q = new int[32768]; //will the memory be allocated immediately ?
But i fear that, doing this may cause lot of memory to be allocated to the int array.
How much, how does one calculate this ?
Is it utterly foolish to create a int array that large in size ? Or is this ok.
Awaiting response
Thanks..
peter greaves
Ranch Hand

Joined: Sep 27, 2002
Posts: 51
>_q = new int[32768]; //will the memory be allocated immediately ?
yes. 32768 32-bit (4 byte) ints will be created, all set to the integral default of 0. so if you like to work in kb, that would be 32768*4/1024 = 128k. (my math is notoriously bad so its worth checking this )
note that you aren't increasing the array size - arrays are immutable, like spock's facial expressions. you are changing the reference _q to point to a new array, leaving the other to be (probably) garbage collected.
maybe you could give some context - why do you need 32k integers? that would help the "best way to go about it" debate to get started.
hth
peter


SJCP 1.2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int array question