• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assigning Arrays  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when I assign different sized int[] arrays to each other?

For example:

int[] small = new int[100];
int[] large = new int[1000];

large = small;

Does the large array get smaller? Will it still hold 1000 integers?

What about going the other direction? Will this throw an exception?

Thanks,

Drew
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Drew,

"large" and "small" are not the arrays themselves; they're pointers or references to arrays. These references don't include any concept of size, just that the pointed-to-thing is an array of integers. When you assign small = large, all you're doing is making the reference that previously pointed to the 10-element array object point to the 100-element array object; the 10-element array is lost, and will be reclaimed by the garbage collector.

you should probably mosey over here and read up on how references work; there are some clever pictures too!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!