Thursday, October 28, 2010

Servlet is server side program .
CGI is written by Pearl .
Servlet is create new thread for every request.

Write servlet need to import below packages
servlet javax.servlet.* and javax.servlet.Http.*

Servlet class has to be public class and it extends from HttpServlet

Servlet has servlet life cycle
init() destroy() - Executed once in life cycle of servlet.
services() - Executed multiple time in life cycle of servlet.
Services Method is executed for every client request.
That means services method has logic for process the client every client request and responses.


What is faster - LinkedList of ArrayList?

As programmers, we often try to eke the last ounce of performance out of our collections. An interesting statement is this: "ArrayList is faster than LinkedList, except when you remove an element from the middle of the list." I have heard this on more than one occasion, and a few months ago, decided to try out how true that statement really was. Here is some code that I wrote during last week's Java 5 Tiger course:
import java.util.*;

public class ListTest {
  private static final int NUM_ELEMENTS = 100 * 1000;
  public static void main(String[] args) {
    List ar = new ArrayList();
    for (int i = 0; i < NUM_ELEMENTS; i++) {
      ar.add(i);
    }
    testListBeginning(ar);
    testListBeginning(new LinkedList(ar));
    testListMiddle(ar);
    testListMiddle(new LinkedList(ar));
    testListEnd(ar);
    testListEnd(new LinkedList(ar));
  }
  public static void testListBeginning(List list) {
    long time = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
      list.add(0, new Object());
      list.remove(0);
    }
    time = System.currentTimeMillis() - time;
    System.out.println("beginning " +
        list.getClass().getSimpleName() + " took " + time);
  }
  public static void testListMiddle(List list) {
    long time = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
      list.add(NUM_ELEMENTS / 2, new Object());
      list.remove(NUM_ELEMENTS / 2);
    }
    time = System.currentTimeMillis() - time;
    System.out.println("middle    " +
        list.getClass().getSimpleName() + " took " + time);
  }
  public static void testListEnd(List list) {
    long time = System.currentTimeMillis();
    for (int i = 0; i < 10000000; i++) {
      list.add(new Object());
      list.remove(NUM_ELEMENTS);
    }
    time = System.currentTimeMillis() - time;
    System.out.println("end       " +
        list.getClass().getSimpleName() + " took " + time);
  }
}
  
One small little addition in Java 5 is the method getSimpleName() defined inside Class. I do not know how many times I have needed such a method and have had to write it.
The output is obvious, but surprising nevertheless in the extent of the differences:
beginning ArrayList took 4346
beginning LinkedList took 0
middle    ArrayList took 2104
middle    LinkedList took 26728
end       ArrayList took 731
end       LinkedList took 1242
  
Finding the element in the middle of the LinkedList takes so much longer that the benefits of just changing the pointer are lost. So, LinkedList is worse than ArrayList for removing elements in the middle, except perhaps if you are already there (although I have not tested that).
So, when should you use LinkedList? For a long list that works as a FIFO queue, the LinkedList should be faster than the ArrayList. However, even faster is the ArrayBlockingQueue or the CircularArrayList that I wrote a few years ago. The answer is probably "never".

ArrayList Vs LinkedList


The Java SDK contains 2 implementations of the List interface - ArrayList and LinkedList.A common design dilemma for developers is when to use what ?

Some basics first:
An arraylist used an array for internal storage. This means it's fast for random access (e.g. get me element #999), because the array index gets you right to that element. But then adding and deleting at the start and middle of the arraylist would be slow, because all the later elements have to copied forward or backward. (Using System.arrayCopy())

ArrayList would also give a performance issue when the internal array fills up. The arrayList has to create a new array and copy all the elements there. The ArrayList has a growth algorithm of (n*3)/2+1, meaning that each time the buffer is too small it will create a new one of size (n*3)/2+1 where n is the number of elements of the current buffer. Hence if we can guess the number of elements that we are going to have, then it makes sense to create a arraylist with that capacity during object creation (using the overloaded construtor or ArrayList)

LinkedList is made up of a chain of nodes. Each node stores an element and the pointer to the next node. A singly linked list only has pointers to next. A doubly linked list has a pointer to the next and the previous element. This makes walking the list backward easier.

Linked lists are slow when it comes to random access. Gettting element #999 means you have to walk either forward from the beginning or backward from the end (depending on whether 999 is less than or greater than half the list size), calling next or previous, until you get to that element.Linked lists are fast for inserts and deletes anywhere in the list, since all you do is update a few next and previous pointers of a node. Each element of a linked list (especially a doubly linked list) uses a bit more memory than its equivalent in array list, due to the need for next and previous pointers.

Ok. Cool...Now that the fundamentals are clear, let's conclude on when to use what:

Here is a snippet from SUN's site.

The Java SDK contains 2 implementations of the List interface - ArrayList and LinkedList.
If you frequently add elements to the beginning of the List or iterate over the List to delete elements from its interior, you should consider using LinkedList. These operations require constant-time in a LinkedList and linear-time in an ArrayList. But you pay a big price in performance. Positional access requires linear-time in a LinkedList and constant-time in an ArrayList.

Friday, October 22, 2010

Java

OOPS Concepts

Encapsulation :
                      An Encapsulation is hiding internal data form
Abstraction :
                    An Abstraction is Manage complexity in simplified Manner .
e.g.
1) Color is combination on 3 main color Red,Green,Blue but for remembering the color we do not need to know what is ratio of RGB for Orange , We are need to know only the Orange is color .
2) Car has made up of thousand of part , but at the time thinking about the car we know only the car as an hole object rather than the Car all part.