# Deleting Objects in Java

## Recommended Posts

It's very frustrating that objects can't be explicitly deleted in Java... I can change the reference and let garbage collection delete it, but this gets tricky when there's more than one reference to the object. Why does Java have this limitation and is there any way to get around it?

Specifically, I'm trying to implement a binary search tree. Ultra-specifically, I'm trying to write the function to delete a node. Deleting a leaf should be easy--just get rid of it. But it's turning out to be the hardest type of node to delete because to "just get rid of it," I have to go back and find the parent node, figure out whether the leaf is on the left or right branch, and set either left or right to null.

I'm not sure whether my instructor intends for us to simply use the built-in class java.util.TreeSet, but while I'm waiting for him to return my email... might as well learn something new, right?

##### Share on other sites

It's very frustrating that objects can't be explicitly deleted in Java... I can change the reference and let garbage collection delete it, but this gets tricky when there's more than one reference to the object. Why does Java have this limitation and is there any way to get around it?

Most garbage collected languages don't let you explicitly free objects. This is because there may still be outstanding references to the object elsewhere in the application, and if you could explicitly free and object these references would become invalid.

Specifically, I'm trying to implement a binary search tree. Ultra-specifically, I'm trying to write the function to delete a node. Deleting a leaf should be easy--just get rid of it. But it's turning out to be the hardest type of node to delete because to "just get rid of it," I have to go back and find the parent node, figure out whether the leaf is on the left or right branch, and set either left or right to null.

This should be fairly easy to do with a recursive function which examines the children of a given node. That's where you should be doing your comparisons. If you're 'backtracking' up the tree you're doing something wrong.

##### Share on other sites

Thanks! After a little thought, I figured out how to implement the function without backtracking (although it still seems unnecessarily complicated to me). I guess all I needed was a bit of a rant...disguised as a civilized computer science question.

##### Share on other sites

• 3 years later...

I just uncovered this post of mine from forever ago, when I was starting to learn programming. 4 years and many real CS courses later, I have no idea what this question means. xD

##### Share on other sites

To remove\free an object that reserve memory in Java, you need to release all of its references

Example:


// reference A points at a new object

Object A = new Object ( );

// reference B points at the same object

Object B = A;

// release reference A

A = null;

// the object is still in memory

B = null;

// now garbage collector will free the object



Another example:


class Node
{
public int data;
public Node next;

public Node (int d, Node n)
{
data = d;
next = n;
}
}

class DS
{

public void push (int d)
{
}

public int pop ()
{
return (t);
}

// free the first reference of the chain
// and chained objects will be removed
// in a sequential order

public reset ()
{
}
}



Edited by khaled
##### Share on other sites

I just uncovered this post of mine from forever ago, when I was starting to learn programming. 4 years and many real CS courses later, I have no idea what this question means. xD

This is always fun. I have some doozies from my younger days.

##### Share on other sites

• 1 month later...

Does a way exist for writing a line that instructs the program to

....just find all the references to an object?

....test or model what behaviors shall occur if you'd free all references to a set of chained objects, but without really freeing them?

## Create an account

Register a new account