Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

March 13, 2011

Volatile variable in java

According to Kathy Sierra :

" The volatile modifier tells the JVM that a thread accessing the variable must
always reconcile its own private copy of the variable with the master copy in
memory "

My understanding :
In normal scenario, In a multi-threaded environment, every thread keeps a local copy of instance variable and if a thread makes some change to the variable, the changed value would be visible to that thread only.

But if we declare a variable as volatile, the variable is stored in main memory and not in local memory of any thread. So, if the value of the variable is changed by any thread, it will be changed in the main memory and would be visible to all threads accessing that variable.

March 9, 2011

The Java Class Loading Mechanism


The Java platform uses a delegation model for loading classes. The basic idea is that every class loader has a "parent" class loader. When loading a class, a class loader first "delegates" the search for the class to its parent class loader before attempting to find the class itself.

Constructors in java.lang.ClassLoader and its subclasses allow you to specify a parent when you instantiate a new class loader. If you don't explicitly specify a parent, the virtual machine's system class loader will be assigned as the default parent.
The loadClass method in ClassLoader performs these tasks, in order, when called to load a class:
If a class has already been loaded, it returns it.
Otherwise, it delegates the search for the new class to the parent class loader.
If the parent class loader does not find the class, loadClass calls the method findClass to find and load the class.
The findClass method of ClassLoader searches for the class in the current class loader if the class wasn't found by the parent class loader. You will probably want to override this method when you instantiate a class loader subclass in your application.
The class java.net.URLClassLoader serves as the basic class loader for extensions and other JAR files, overriding the findClass method of java.lang.ClassLoader to search one or more specified URLs for classes and resources.

forward vs sendRedirect

RequestDispatcher.forward() :

1) Forwards the request to some other servlet/jsp within the same application.
2) The URL in the browser does not change so the client does not get to know that the request is being sent to some other servlet. The process occurs completely within the web container
3) Request attributes are maintained
4) Faster, since everything is at the server side only

Response.sendRedirect()
1) Can forward request to a resource in some other application as well
2) The URL in the browser changes. So the web container returns to the browser indicating that the a new URL should be requested.
3)  Since a new request object is created, any request attributes already stored will be destroyed.
4) Slower, since the round trip to client is involved

java.lang.Error

Error extends Throwable.

It indicates that some serious and abnormal condition has occurred and the application should not try to catch it.

March 4, 2011

Multiple catch blocks



Both

try{

     }catch(Error e){

     }catch(NullPointerException npe){

    }

and


           try{

}catch(NullPointerException npe){

}catch(Error e){

}

will compile.

Exception Hierarchy



Throwable is a class which extends Serializable interface.
All other classes in hierarchy extend Throwable class

Error , Exception and Runtime Exception classes only have the constructors (which call the corresponding super class constructors). All other methods are inherited from the Throwable class itself.