Heap and delete | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Heap and delete

In c++ if I declared a variable or array in the heap.. it's better to dalete them so the computer won't crash after a while ( delete d; ) for example. Do I have to fa the same in JAVA? If yes.. how can I do it?

16th Jan 2017, 11:56 PM
Layla A Hammoury
Layla A Hammoury - avatar
2 Answers
+ 2
Thank you
17th Jan 2017, 10:32 AM
Layla A Hammoury
Layla A Hammoury - avatar
+ 1
In Java, all objects are defined in the heap. But java has a garbage collector to free any memory space that has no reference to it. So you can tell the garbage collector to collect an object by removing its all references. For example : myObject = null; The garbage collector will collect the object from memory, not immediately but it will.
17th Jan 2017, 2:04 AM
Ahmed Elzoughby
Ahmed Elzoughby - avatar