Pages

Monday 23 February 2015

JAVA QUESTIONS

What is difference between Executor.submit() and Executer.execute() method ?

There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, 
checked exception or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get() will re-throw this exception, wrapped in an ExecutionException.


Define Serialization? What do you mean by Serialization in Java?

Serialization is a mechanism by which you can save or transfer the state of an object by converting it to a byte stream. This can be done in java by implementing Serialiazable interface. Serializable is defined as a marker interface which needs to be implemented for transferring an object over a network or persistence of its state to a file. Since its a marker interface, it does not contain any methods. Implementation of this interface enables the conversion of object into byte stream and thus can be transferred. The object conversion is done by the JVM using its default serialization mechanism.

DAY:4
What is Marker interface? How is it used in Java?
The marker interface is a design pattern, used with languages that provide run-time type information about objects. It provides a way to associate metadata with a class where the language does not have explicit support for such metadata. To use this pattern, a class implements a marker interface, and code that interact with instances of that class test for the existence of the interface. Whereas a typical interface specifies methods that an implementing class must support, a marker interface does not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. There can be some hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used.
 Java utilizes this pattern very well and the example interfaces are:
·        java.io.Serializable
·        java.rmi.
·        java.lang.
·        javax.servlet.SingleThreadModel
·        java.util.EvenListener
The "instanceof" keyword in java can be used to test if an object is of a specified type. So this keyword in combination with Marker interface can be used to take different actions based on type of interface an object implements.

What is the difference between final, finally and finalize?
 “final” is the keyword to declare a constant AND prevents a class from inherting subclasses. And overriding  super class methods.
“finally” is a block of code that always executes when the try block is finished, unless System.exit() was called.
“finalize()” is an method that is invoked before an object is discarded by the garbage collector.


No comments:

Post a Comment