Monday, August 4, 2008

FAQ How many different types of JDBC drivers are present

How many different types of JDBC drivers are present? Discuss them.

There are four JDBC driver types.
Type 1: JDBC-ODBC Bridge plus ODBC Driver:
The first type of JDBC driver is the JDBC-ODBC Bridge. It is a driver that provides JDBC access to databases through ODBC drivers. The ODBC driver must be configured on the client for the bridge to work. This driver type is commonly used for prototyping or when there is no JDBC driver available for a particular DBMS.
Type 2: Native-API partly-Java Driver:
The Native to API driver converts JDBC commands to DBMS-specific native calls. This is much like the restriction of Type 1 drivers. The client must have some binary code loaded on its machine. These drivers do have an advantage over Type 1 drivers because they interface directly with the database.
Type 3: JDBC-Net Pure Java Driver:
The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network protocol that is sent to a middleware server. This server then translates this DBMS-independent protocol into a DBMS-specific protocol, which is sent
to a particular database. The results are then routed back through the middleware server and sent back to the client. This type of solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client.
Type 4: Native-Protocol Pure Java Driver
These are pure Java drivers that communicate directly with the vendor's database. They do this by converting JDBC commands directly into the database engine's native protocol. This driver has no additional translation or middleware layer, which improves performance tremendously.
What does the "static" keyword mean in front of a variable? A method? A class? Curly braces {}?
static variable
- means a class level variable
static method:
-does not have "this". It is not allowed to access the not static members of the class.
can be invoked enev before a single instance of a class is created.
eg: main
static class:
no such thing.
static free floating block:
is executed at the time the class is loaded. There can be multiple such blocks. This may be useful to load native libraries when using native methods.
eg:
native void doThis(){
static{
System.loadLibrary("myLibrary.lib");
}

Access specifiers: "public", "protected", "private", nothing?

In the case of Public, Private and Protected, that is used to describe which programs can access that class or method: Public – any other class from any package can instantiate and execute the classes and methods. Protected – only subclasses and classes inside of the package can access the classes and methods. Private – the original class is the only class allowed to executed the methods.

What does the "final" keyword mean in front of a variable? A method? A class?

FINAL for a variable : value is constant
FINAL for a method : cannot be overridden
FINAL for a class : cannot be derived
A final variable cannot be reassigned,
but it is not constant. For instance,
final StringBuffer x = new StringBuffer()
x.append("hello");
is valid. X cannot have a new value in it,but nothing stops operations on the object
that it refers, including destructive operations. Also, a final method cannot be overridden
or hidden by new access specifications.This means that the compiler can choose
to in-line the invocation of such a method.(I don't know if any compiler actually does
this, but it's true in theory.) The best example of a final class is
String, which defines a class that cannot be derived.

Does Java have "goto"?

No.

What synchronization constructs does Java provide? How do they work?

The two common features that are used are:
1. Synchronized keyword - Used to synchronize a method or a block of code. When you synchronize a method, you are in effect synchronizing the code within the method using the monitor of the current object for the lock.
The following have the same effect.
synchronized void foo() {
}
and
void foo() {
synchronized(this) {
}
If you synchronize a static method, then you are synchronizing across all objects of the same class, i.e. the monitor you are using for the lock is one per class, not one per object.
2. wait/notify. wait() needs to be called from within a synchronized block. It will first release the lock acquired from the synchronization and then wait for a signal. In Posix C, this part is equivalent to the pthread_cond_wait method, which waits for an OS signal to continue. When somebody calls notify() on the object, this will signal the code which has been waiting, and the code will continue from that point. If there are several sections of code that are in the wait state, you can call notifyAll() which will notify all threads that are waiting on the monitor for the current object. Remember that both wait() and notify() have to be called from blocks of code that are synchronized on the monitor for the current object.

Does Java have multiple inheritance?

Java does not support multiple inheritence directly but it does thru the concept of interfaces.
We can make a class implement a number of interfaces if we want to achieve multiple inheritence type of functionality of C++.

How does exception handling work in Java?

1.It separates the working/functional code from the error-handling code by way of try-catch clauses.
2.It allows a clean path for error propagation. If the called method encounters a situation it can't manage, it can throw an exception and let the calling method deal with it.
3.By enlisting the compiler to ensure that "exceptional" situations are anticipated and accounted for, it enforces powerful coding.
4.Exceptions are of two types: Compiler-enforced exceptions, or checked exceptions. Runtime exceptions, or unchecked exceptions. Compiler-enforced (checked) exceptions are instances of the Exception class or one of its subclasses — excluding the RuntimeException branch. The compiler expects all checked exceptions to be appropriately handled. Checked exceptions must be declared in the throws clause of the method throwing them — assuming, of course, they're not being caught within that same method. The calling method must take care of these exceptions by either catching or declaring them in its throws clause. Thus, making an exception checked forces the us to pay heed to the possibility of it being thrown. An example of a checked exception is java.io.IOException. As the name suggests, it throws whenever an input/output operation is abnormally terminated.