Sunday, March 30, 2008

CORE JAVA FAQS

16. What is a singleton class and how to implement it? What are the advantages of Singleton class?

Singleton classes are created to have only one instance.
A singleton may be used to represent some unique system.
It is generally implemented by keeping the constructor private.
It should have static members and methods.
The main advantage of singleton class is memory management( Garbage collection)

17. Why java prohibits use of ‘synchronized’ inside a constructor ?

Because other threads cannot get a reference to the object until construction of the object completes. This has got a performance overhead.

18. What do you mean by Garbage collection and what are its advantages and disadvantages?

The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources may be reclaimed.

An object is eligible for garbage collection when its reference is set to null. Method variables or local variables are eligible for Garbage collection when they go out of scope.

Garbage Collection cannot be forced explicitly. JVM may do garbage collection if it is running short of memory.The call System.gc() does NOT force the garbage collection but only suggests that the JVM may make an effort to do garbage collection.

It frees up the heap space and takes care of heap fragmentation.

It also helps to ensure program integrity as programmers are unable to crash the JVM by incorrectly freeing the memory accidentally or purposefully.

The only disadvantage is garbage-collected heap is a performance overhead.

19.What do you mean by conservative garbage collector?

The garbage collectors that cannot distinguish between genuine object references and look-alikes.

20. Can an object's finalize () method be invoked while it is reachable?

An object's finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object's finalize() method may be invoked by other objects.

0 comments: