Saturday, February 26, 2011

Convert String array to ArrayList

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class StringArrayTest
   public static void main(String[] args)
      String[] fruits = {"apple", "banana", "coconut", "date", "eggplant"};
      List<String> fruitList = Arrays.asList(fruits);
      for (String str : fruitList)

If you could click on some of the google ads you see on the right side. It will help me to run this blog and motivates me ;)

Thursday, February 24, 2011

MVC Architecture

The main aim of the MVC architecture is to separate the business logic and application data from the presentation data to the user.

Here are the reasons why we should use the MVC design pattern.

  1. They are resuable : When the problems recurs, there is no need to invent a new solution, we just have to follow the pattern and adapt it as necessary.
  2. They are expressive: By using the MVC design pattern our application becomes more expressive.

1). Model: The model object knows about all the data that need to be displayed. It is model who is aware about all the operations that can be applied to transform that object. It only represents the data of an application. The model represents enterprise data and the business rules that govern access to and updates of this data. Model is not aware about the presentation data and how that data will be displayed to the browser.

2). View : The view represents the presentation of the application. The view object refers to the model. It uses the query methods of the model to obtain the contents and renders it. The view is not dependent on the application logic. It remains same if there is any modification in the business logic. In other words, we can say that it is the responsibility of the of the view's to maintain the consistency in its presentation when the model changes.

3). Controller: Whenever the user sends a request for something then it always go through the controller. The controller is responsible for intercepting the requests from view and passes it to the model for the appropriate action. After the action has been taken on the data, the controller is responsible for directing the appropriate view to the user. In GUIs, the views and the controllers often work very closely together.

Difference between Model 1 and Model 2 architecture:

Features of MVC1:

  1. Html or jsp files are used to code the presentation. To retrieve the data JavaBean can be used.
  2. In mvc1 archictecture all the view, control elements are implemented using Servlets or Jsp.
  3. In MVC1 there is tight coupling between page and model as data access is usually done using Custom tag or through java bean call.

Features of MVC2:

  1. The MVC2 architecture removes the page centric property of MVC1 architecture by separating Presentation, control logic and the application state.
  2. In MVC2 architecture there is only one controller which receives all the request for the application and is responsible for taking appropriate action in response to each request.

What is the difference between servlets and Java Server Pages? Which is preferred?

Servlets and Java Server Pages are complementary APIs, both providing a means for generating dynamic Web content. A servlet is a Java class implementing the javax.servlet.Servlet interface that runs within a Web or application server's servlet engine, servicing client requests forwarded to it through the server. A Java Server Page is a slightly more complicated beast. JSP pages contain a mixture of HTML, Java scripts (not to be confused with JavaScript), JSP elements, and JSP directives. The elements in a Java Server Page will generally be compiled by the JSP engine into a servlet, but the JSP specification only requires that the JSP page execution entity follow the Servlet Protocol.

The advantage of Java Server Pages is that they are document-centric. Servlets, on the other hand, look and act like programs. A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content. Some of the JSP functionality can be achieved on the client, using JavaScript. The power of JSP is that it is server-based and provides a framework for Web application development. Rather than choosing between servlets and Java Server Pages, you will find that most non-trivial applications will want to use a combination of JSP and servlets. In fact, the JSP 1.1 and Servlet 2.2 specifications are based around the concept of the Web application, combining the two APIs into a unified framework.

Amit Ranjan
Java developer
P please consider the environment before printing e-mail-- SAVE PAPER!

Tuesday, February 8, 2011

Why ArrayList's Default size is 10?

ArrayList uses an array to store the elements. Arrays have a fixed size. The array that ArrayList uses has to have a default size, obviously. 10 is probably a more or less arbitrary number for the default number of elements. When you create a new ArrayList with nothing in it, then ArrayList will have made an array of 10 elements behind the scenes. Ofcourse those 10 elements are all null.

Every time the array is full, ArrayList creates a new, larger array and copies the elements from the old array to the new array. You don't want that to happen every time that you add an element to the ArrayList (copying the array takes time, especially if the array is large), so ArrayList does it in steps of 10 or so.

If you want to know exactly how it works, look for the file in your JDK installation directory. Open it and lookup the source code for java.util.ArrayList in there.


If you could click on some of the google ads you see on the right side. It will help me to run this blog and motivates me ;)

Default size:

Vector - 10 Arraylist - 10 Hashtable - 11 Hashmap - 16 Hashset - 16
send me response about
what is the default size of StringBuffer

Select random row from database

Different ways to select row from a database table randomly. SQL statement for different databases as follows:

Select a random row with MySQL:

SELECT column FROM table

Select a random row with PostgreSQL:

SELECT column FROM table

Select a random row with Microsoft SQL Server:

SELECT TOP 1 column FROM table

Select a random row with IBM DB2

SELECT column, RAND() as IDX 
FROM table 

Select a random record with Oracle:

( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Amit Ranjan
Java developer
P please consider the environment before printing e-mail-- SAVE PAPER!