The Artima Developer Community
Sponsored Link

Java Answers Forum
delete objects;

6 replies on 1 page. Most recent reply: Nov 9, 2011 1:05 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Dimitris Paras

Posts: 12
Nickname: dimpars
Registered: Feb, 2005

delete objects; Posted: Feb 24, 2005 2:00 AM
Reply to this message Reply
Advertisement
I am a c++ developer and i used to delete ojects with

object *obj=new object;
delete obj;

What shall i do in java, to delete an object from a linkedlist? for example

customer cc=(customer)(customers.getFirst());

Is it enough to remove it like this:

Customers.removeFirst();

Thank you in advance for your consideration!


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: delete objects; Posted: Feb 24, 2005 3:22 AM
Reply to this message Reply
Hi,

Don't worry about deleting an object in Java. Garbage Collector (GC) will take care of it and object will be deleted.
Search for articles on GC on google to more about how GC works or best way is to read chapter 9 of "Inside JVM". Here is the online version http://www.artima.com/insidejvm/ed2/index.html

In C++ we have to explicitly delete object because their is not GC.

Regards,
Amol Brid.

Dimitris Paras

Posts: 12
Nickname: dimpars
Registered: Feb, 2005

Re: delete objects; Posted: Feb 24, 2005 7:47 AM
Reply to this message Reply
ok thanks a lot!!!

perry anderson

Posts: 10
Nickname: anderson
Registered: Feb, 2005

Re: delete objects; Posted: Feb 28, 2005 1:09 PM
Reply to this message Reply
actually, that's not exactly the full story

there is an implied way to remove the object from memory which comes in handy if your allocating alot of objects and need that space freed up as soon as possible


myObject = new MyClass();
myVector.add(myObject);

...
myVector.remove(myObject);
myObject = null;


the myObject = null drops a hint to the garabage collector that it's ok to free that objects space right away... which it usually does otherwise what happens is that unreferenced objects get collected till memory is needed and then a full gc process kicks in, pausing all operations until it's done. by helping out the gc with this simple operation, you are actually helping yourself by removing the need for a full garabage collection at a later time.

it works beautifully and came in really handy when i was developing an audio/video conferencing app in java and did not need the gc going bizzerk every five minutes....

- perry

perry anderson

Posts: 10
Nickname: anderson
Registered: Feb, 2005

Re: delete objects; Posted: Feb 28, 2005 3:25 PM
Reply to this message Reply
public class MyClass() {
 
   public void static main(String[] args) {
 
     myObject = new MyClass();
     myVector.add(myObject);
 
     ...
 
     myVector.remove(myObject);
     myObject = null;
 
  }
 
}
 


hmmmm, that's nice

- perry

Ronan Masangcay

Posts: 1
Nickname: ronmass
Registered: Nov, 2011

Re: delete objects; Posted: Nov 6, 2011 7:19 PM
Reply to this message Reply
Hi! Sorry am quite new to JAVA. Please explain where did myVector come from?

Thanks,

Ronan

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: delete objects; Posted: Nov 9, 2011 1:05 AM
Reply to this message Reply
It didn't come from anywhere. The code example is just an simple example that would fail to compile.

myVector should have been declared before it was used, either in the method:
Vector myVector = new Vector();
or outside the method but inside the class:
private Vector myVector = new Vector();
(For what it's worth, the Vector class is considered obsolete and not used.)

Flat View: This topic has 6 replies on 1 page
Topic: Set length of an int. Previous Topic   Next Topic Topic: Drawing tress/graph in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use