Showing posts with label Java Interview Questions and Answers. Show all posts
Showing posts with label Java Interview Questions and Answers. Show all posts

Friday, December 12, 2014

Generics in Java

Generics:

Type Safety Objects: Allow one type of object, does not allow other object

No Casting required.

For example in Collection

List<String> list = new ArrayList<String>
Allow only string objects to List.

Map<String,Integer> map = new TreeMap<String,Integer>
Allow String and Integer to Map.


When we try to add number it will throw compile time.

General Generic Example

class Generic<T>{
T object;

void add(T object)
{
              this.object=object;
        }

T get()
{
            return object;
        }
public static void main(String[] args) {
     Generic<Integer> ingen = new Generic<Integer>();
     Generic<String> strgen = new Generic<String>();
 
     ingen.add(new Integer(11));
     strgen.add(new String("Generic"));

     System.out.printf("Integer =%d", ingen.get());
     System.out.printf("String =%s", strgen.get());
  }

}

Print Integer =11  String =Generic.

Wednesday, August 22, 2012

Thread Example in Java


public class ThreadExample extends Thread{

public synchronized  void run(){
for(int i=0;i<11;i++)
System.out.print(i);
System.out.println(" ");
}
public static void main(String args[]){
Thread t1=new ThreadExample();
Thread t2=new ThreadExample();
Thread t3=new ThreadExample();

t1.start();
t2.start();
t3.start();

t1.setPriority(MAX_PRIORITY);
System.out.println("Thread 1 Priority"+t1.getPriority());

System.out.println("Thread 1 Group"+t1.getThreadGroup());

System.out.println("State of Thread2"+t2.getState());

}
}

Monday, August 20, 2012

Thread Interview Questions in Java


1) What is the difference between checked and unchecked exception handling in Java? What are the disadvantages of checked exception handling?
Ans: Checked exceptions are the one for which there is a check by the compiler that these exceptions have to be caught or specified with throws keyword. These kind of exceptions occur because of conditions which are out of control of the application like Network error, File Access Denied etc.
Unchecked exceptions are the one which arise because of logic written by the developer. e.g. Trying to access an array element which doesn’t exist.

Multiple exceptions in single catch block
catch(FileNotFoundExcpetion fne | Exception e){}

What are the differences between NoClassDefFoundError and ClassNotFoundException?
Ans: NoClassDefFoundError occurs when a class was found during compilation but could not be located in the classpath while executing the program.
For example: class A invokes method from class B and both compile fine but before executing the program, an ANT script deleted B.class by mistake. Now on executing the program NoClassDefFoundError is thrown.
ClassNotFoundException occurs when a class is not found while dynamically loading a class using the class loaders.
For example: The database driver is not found when trying to load the driver using Class.forName() method.

What is the purpose of throw keyword? What happens if we write “throw null;” statement in a Java program?
Ans: ”throw” keyword is used to re-throw an exception which has been caught in a catch block. The syntax is “throw e;” where e is the reference to the exception being caught. The exception is re-thrown to the client.
This keyword is useful when some part of the exception is to be handled by the caller of the method in which throw keyword is used.
The use of “throw null;” statement causes NullPointerException to be thrown.
The answers are brief. Do post your comments if you think I have missed anything or discuss any other exception handling question.