Jump to content

Deleting Objects in Java


Willa

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? :D

Link to comment
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.

Link to comment
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. :P

Link to comment
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

Link to comment
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
{
   Node head = null;

   public void push (int d)
   {
       head = new Node (d, head);
   }

   public int pop ()
   {
       int t = head.data;
       head = head.next;
       return (t);
   }

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

   public reset ()
   {
       head = null;
   }
}

Edited by khaled
Link to comment
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.

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.