+ 1
Is there a waste of memory because of the bad manipulation of Referances??
If i write this code: String str=new String("hello"); str = new String("World"); Is there a waste of memory because of the bad manipulation of Referances? And please Why??
2 Answers
+ 1
Java has something called the garbage collector.
When str is no longer referencing "Hello", "Hello" is then considered garbage since it doesn't have a reference and can no longer be accessed. "Hello" is then deleted by the garbage collector. So no it isnt a waste of memory since its deleted. The garbage collector is automatically on, you dont have to import it or do it manually.
I don't understand it that well so thats the best I can explain it to my knowedge, search it up I
if you want a better explanation.
+ 3
"hello" will remain in memory until the GC removes it
but the GC doesn't work continuously, so garbages like this accumulates for some time.