Friday, November 18, 2005

Java



Everything picked up from
"http://www.allapplabs.com/interview_questions/java_interview_questions.htm"

What is MultiThreading and synchronization? ( From the NET)
"With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors."

Different ways of using thread?
"
The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance..the only interface can help."

Difference Between array and vectorlist?
Vector is synchronized whereas arraylist is not.

Final in java?
  • A final class can't be extended ie., final class may not be subclassed.
  • A final method can't be overridden when its class is inherited.
  • You can't change value of a final variable (is a constant).
What is serialization?
  • Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.
  • Whenever an object is to be sent over the network, objects need to be serialized. Moreover if the state of an object is to be saved, objects need to be serilazed
  • One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.
Transient?
This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

Different ways to handle exceptions?
There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.


In the first approach as a programmer of the method, you urself are dealing with the exception. This is fine if you are in a best position to decide should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it's own exceptions, then do not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. You will find the same approach throughout the java libraries we use.

Some Gotchas..
If I write return at the end of the try block, will the finally block still execute
Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.

If I write System.exit (0); at the end of the try block, will the finally block still execute?
No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.


No comments: