• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(re)assigning arrays?

 
Ender Everett
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about (re)assigning array reference variables.
If I create two arrays, I can reassign the reference variable name of one of them to the other array. No problem... I am curious, though, if I can ever reassign the reference variable to the original array, or if it is possible to assign a new reference variable to the now nameless array. If I can, how? It looks like the unassigned array is just floating out there in space waiting for the garbage collector to come by...
Here is code of a sample situation:

OK, So I reassigned the reference variable nums2. Now can I ever re-link to the array that was originally referenced through nums2 in any way? Is that whole object just lost now (as far as the program is concerned) and liable to be trashed at any time or... ? Just curious.
I have absolutely zero practical application for this at the present time... I just started studying Java about 10 days ago, and I am curious about the little tiny specifics of implementation.
To be totally up front, this is my first go at progamming ever, and this is all quite fascinating to me...
Anyway, thanks!
 
Tom McQueary
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you have correctly analyzed the situation. Since you assigned a new value to the reference variable that originally held the reference to the array of negative numbers, you lost your only way of accessing this array. Since the program cannot access this array, the array will eventually be garbage collected.
 
Ender Everett
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah... OK. Thanks! Now I have to go find some practical application where it is useful to de-link and let an array slip off into the trash bin. ??
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to save the new array from the garbage is to create another reference to it before you reassign the reference...

 
Ender Everett
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about that.
But why would I ever want to do this? Is there ever any practical or realworld reason to do it?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic