Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int array question

 
Rojer Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>_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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic