Results 1 to 3 of 3

Thread: can any one tell me the basic difference b/w destructor in c,c++ &finalizer in java?

  1. #1
    Join Date
    Feb 2008
    Posts
    1

    Default can any one tell me the basic difference b/w destructor in c,c++ &finalizer in java?

    computer programming
    Powered by Yahoo!

  2. #2
    Join Date
    Feb 2008
    Posts
    1

    Default

    Destructors don't exist in C, just in C++.

    I believe you need destructors in C++ to do things like deallocate memory for arrays your objects generated dynamically.

    In Java, all your memory gets deallocated automatically when you stop using it. You use finalizers to stop using system resources, like file streams and network sockets. There are usually better ways to do this (like closing a stream in the finally clause of the try-catch block the stream is used in).

    Only use finalizers when necessary.

    I don't think you're supposed to use C++ destructors to stop using system resources, but you probably could if you wanted to.
    Powered by Yahoo!

  3. #3
    Join Date
    Feb 2008
    Posts
    1

    Default

    destructor in c/ c++

    As opposed to a constructor, a destructor is called when a program has finished using an instance of an object. A destructor does the cleaning behind the scenes. Like the default constructor, the compiler always create a default destructor if you don't create one. Like the default constructor, a destructor also has the same name as its object. This time, the name of the destructor starts with a tilde.

    and in destructor in Java

    the finalize() method is called before the object is to gced. Note that the invocation of finalize is not gantenteed to be called at any paticular time. And may not be called if System.exit(int) is invoked before the object is gced. If you want an operation to peform on exit use Runtime.addShutdownHook method. If you want to add a destructor to an existing object that you are not implementing you can use a PhantomReference to get notified when the object is dead.

    To add a destructor use:

    protected void finalize() throws Throwable
    {
    //do finalization here
    super.finalize(); //not necessary if extending Object.
    }
    Powered by Yahoo!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •