I'm still learning basics of Java and try to understand that language on different levels. I have a question regarding to creating/deleting old objects in Java.
I have got an example:
Let say that I have got laptopList = new ArrayList<Laptop>;
which contains objects of class Laptop
. We have got 20 created objects, all added to the ArrayList. Now we want to update fourth index with a new Laptop object. In this case we can create new Laptop object and use laptopList.set(4, new Laptop("Dell",1024));
.
In this point I would like to ask you -> We will not use that old instance anymore, but it still exists in memory. Should we set old instance to null
value (or somehow else delete it)? I know for PC's it might be no problem but what impact has it to mobile devices (memory use => faster discharging of battery).
I know that in Java is implemented tool called Garbage Collector. Does it work in this case? Does it delete non use instances immediately? How and when it knows that particular instance will not be used anymore?
Thank you in advance for your response and information.