This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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!
Don't sweat the petty stuff, just pet the sweaty stuff.
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.
Joined: Mar 31, 2001
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. ??