What is a garbage collector in java?
When you instantiate something, like an object, you ask the computer to give you a small amount of memory to store the instantiation. The garbage collector free this memory when you do not need it anymore (but it is far from perfect)
could you explain more with example?
In some languaje like C or C++ there is no garbage collector, it means you are the garbage collector and there is a destructor in other languajes like java it has it's own garbage collector who is encharge of look what is been used and what is not. In java you just create and java delete not used objects but there is a reason why java can do it, it has a virtual machine and the code is precompiled it make that situations are more predictables. If you are a good C C++ programmer there is a huge efficiency difference becouse you control the moment you want to delete objects, but java has kind of daemon(collector) that is running and looking always it is not efficient. Sorry for my english.
Example: int number = 5; //here we declaring a variable System.out.println(number);//here we use that variable System.out.println("variable is no more"); /*variable was collected by the garbage collector and memory was freed, because we don't use it anymore.*/
that means that garbage collector deals only with object