Saturday, October 1, 2011

What do you understand by Synchronization?

Synchronization :

   Two or more threads trying to access the same method at the same point of time leads to synchronization. If that method is declared as Synchronized, only one thread can access it at a time. Another thread can access that method only if the first thread's task is completed.

Example:

Synchronizing a function:

public synchronized void Method () {
// Appropriate method-related code.
}
Example:

Synchronizing a block of code inside a function:

public myFunction (){
synchronized (this) {
// Synchronized code here.
}
}

More about Synchronization click here

What's the difference between constructors and normal methods?

Constructor:
It is automatically invoked when an object is created of a class. It has the same name of its class. Constructor is invoked by using new operator and it has no return type. A constructor can be overloaded but can not be overridden. Default constructor is automatically generated by compiler if class does not have once.

Example: 

Class A
{
     A()
  {
    System.out.println( " this is an example of constructor" );
  }
}
 
Method:

It is just an ordinary member function in a class. Method is invoked by using a dot(.) operator. It has its own name and return type.

Example:

class A
{
    voidDisplay()
  {
      System.out.println(" This is an example of method ");
  }
}

Find more about this question click here

How you can force the garbage collection?

It is one of the best resources java have. Garbage collection comes under memory management.You can not force garbage collection process. You can possibly send a request by calling System.gc(). But it is not quite sure that this request will work. whenever the memory heap is full JVM in built call finalize() method. It will do the clean up operations.
Find further more about garbage collection click here

Difference between this and super?

This is used to invoke constructor of same class.
Super is used to invoke constructor of super class.


Example:


 class Two extends One
{
   Protected String name;
   public Two(String name)
      {
          super( "super class" );
          this.name = name; 
      }
}

 Find more about this and super please click here