Tuesday, 29 July 2014

Different between sleep() method and wait() methods in java?

Both the sleep() and wait() methods are used to suspend a thread execution for a specified time.
When sleep() is executed inside the synchronized block, the object is still under lock.
When wait() is executed , it breaks the synchronized block, so that the object lock is removed and it is available.

 Generally, sleep() is used for making a thread  to wait for some time. But wait() is used in connection with notify() or notifyAll() methods in thread communication. 

Different between Method Overloading and Overriding?

Method Overloading:

1. Writing two are more methods with the same name but different signature is called method Overloading.
2. Method Overloading is done in the same class.
3. In Method Overloading method return type can be same or different.
4. JVM decides which method is called depending on the difference in the method signature.
5. Method Overloading is done when the programmer want to extend the already available feature.
6. Method Overloading is code refinement. same method is refined to perform a different task.

Method Overriding:

1. Writing two are more methods with the same name and same signature is called method Overriding.
2.  Method Overriding is done in super and sub classes.
3. In Method Overriding method return type should be same.
4. JVM decides which method is called depending on the data type of the object used to call the method.
5. Method Overriding is done when the programmer wants to provide a different implementation for the same     features.
6.  Method Overriding is code replacement. the sub class method overrides the super class method.

What are Checked and Unchecked Exception?

Checked Exception:

The exception that are checked by java compiler for execution of the program at the run time are called Checked exception.

Unchecked Exception:

The exceptions which are not checked by java compiler are called Unchecked Exceptions.

Whether exceptions are Checked or Unchecked compulsory it should be runtime only.

errors and it's child classes are Unchecked exceptions and all remaning are Checked exceptions.

Monday, 28 July 2014

What is different between Comparator and Comparable interfaces?

Comparator:
1. we can use the comparator to difine cutomized sorting order.
2. This interface present in java.util package..
3. This interface having two methods.
         compare()
         equals()
4. No predefined class implements the Comparator interface
                                                                                                    
Comparable:
        
1. we can use comparable to define default natural sorting order.
2. This interface present in java.lang package.
3. It defines only one mthod that is 
        CompareTo()
4. All wrapper classes and String class implements Comparable interface.                         

What is the Collection?

Collection is an interface.
Group of individual objects as a single entity is colled Collection.

Why String Objects are immutable?

In the case of String several references pointing to the Same object. By using one reference , if we are performing any change in the existing object the remaining references will be impacted. To resolve this problem  SUN people declared as String objects are immutable. According to this if we trying to perform any changes with those changes a new object is created.

What is the use of transient keyword in java?

 At the time of Serialization if we don't want to serialize the value of partitcular variable to meet the security contraint we have to declare those variable with transient keyword.

At the time of Serialization JVM ignores original value of transient variable and saves default value.


class T {
transient int a; // will not persist
int b; // will persist
}

Example:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class Logon implements Serializable {
private Date date = new Date();
private String username;
private transient String password;
public Logon(String name, String pwd) {
username = name;
password = pwd;
}
public String toString() {
String pwd = (password == null) ? "(n/a)" : password;
return "logon info: \n username: " + username + "\n date: " + date
+ "\n password: " + pwd;
}
public static void main(String[] args) throws Exception {
Logon a = new Logon("Hulk", "myLittlePony");
System.out.println("logon a = " + a);
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
"Logon.out"));
o.writeObject(a);
o.close();
Thread.sleep(1000); // Delay for 1 second
// Now get them back:
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"Logon.out"));
System.out.println("Recovering object at " + new Date());
a = (Logon) in.readObject();
System.out.println("logon a = " + a);
}
}

Friday, 4 July 2014

Why NullPointerExcepton occur in java?

     NullPointerException is Situation in code where you try to access or modify an Object which has not been initialized yet.

  public class NPEException{
  public static void main(String args[]){
      String s="null";
      System.out.println("s.toSting()");
     }
  }


What is the Abstraction?

           A class contain lot of data and the user does not need entire data. The User required only some part of the available data. In this case, we can hide the unnecessary data from the user  and expose only that data is of interest to the user. This is called Abstraction.

Example:
           A bank clerk should see the customer details like account no.r, name and balance of amount in the account. He should not see the entire sensitive data like the staff salaries, profit or loss of the bank, interest of amount paid by the bank and etc.. So such data can be abstracted from the clerk's. Where as the bank manager is interested to know this data it will be provided by the manager.

Advantage:
           The advantage of abstraction is the every user will get his own view of the data according his requirement and he will not get confused with unnecessary data.
           

Thursday, 3 July 2014

What is the Difference between StringBuffer and StringBuilder?

StringBuffer:

  1. In StringBuffer, Every method is Synchronized.
  2. StringBuffer object is thread safe. Because StringBuffer object can be accessed only one thead       at time.
  3. Relatively performance is low compare to StringBuilder.

StringBuilder:

   1. In StringBuilder no mehtod is Synchronized.
   2. StringBuilder is not Thread safe. Because it can be accessed multiple threads simultaneously.
   3. Relatively performance is high compare to StringBuffer.

java4suresh.blogspot.in

Wednesday, 2 July 2014

Diffrent between protected and default packages?

      If member declared as protected then we can access that member with in the current package any where but out side package only in child classes.
      With in the current package we can access protected members either by parent reference or by child reference.
       But from out side package we can access protected members only by using child reference. if we are trying to use parent reference we will get the error.

       If member declared as the default, then we can access that member Only with in the current package
& we can't access from out side of the package, hence default access is also known as package level access.(Here Member means variables and methods)

What is the static polymorphism and Dynamic polymorphism?

   Static polymorphism is the polymorphism exhibited at compile time. Here java compiler knows which
method is called. Method overloading and method overriding using static methods and method overriding using private or final methods are examples for static polymorphism.
   
    Dynamic polymorphism is  the polymorphism exhibited at runtime. Here java compiler does not understand which method is called at compile time. Only JVM decides which method is called at runtime. Method overloading and method overriding using instace methods are examples for Dynamic polymorphism.