Two Laptop Bag
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "int array question" Watch "int array question" New topic

int array question

Rojer Smith

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
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.

SJCP 1.2
I agree. Here's the link:
subject: int array question
It's not a secret anymore!