Monday 22 July 2013

What is the difference between GET and POST method in java?


Get and Post are both the method of HTML form element and they are used to transfer data with the request when the submit button is pressed as:
 

<form action="firstServlet" method="get">     

         <table>

            <tr> <td>

                 <input type="Text" name="UsrName"/>

            </td> </tr>

            <tr><td>

                 <input type="Text" name="pswd"/>

           </td> </tr>

            <tr><td>

                 <input type="submit" name="submit" value="Submit"/>

           </td></tr>

        </table>

</form>

In the above code we have two text box’s  user name and password and one submit button, When we click on the submit button after putting values in the user name and password boxes the control will transfer to the “firstServlet” servlet with the user name and password entered.

The main differences between GET and POST method are as follow.

 

GET

POST

Get request contents are visible in URL

Post request contents are embedded in the body of HTTP request

Get method can only contain data up to 255k

NO such limit for post method

Get request can be cached

Post request contents are not a part of URL so they cannot be cached

Get requests are usually faster than post requests

They are slower than get because they need some extra time to encapsulate in the body of request

Get is a by default method of HTML form element

Post method need be specified for use

Get request can only contain the text data

Post can contain text as well as binary data

 

What is Assertion in Java?




According to Sun, we can use assertion to test our assumption about programs. That means it validates our program!
In another words we can say that assertions ensures the program validity by catching exceptions and logical errors. They can be stated as comments to guide the programmer. Assertions are of two types:

1.       Preconditions: Preconditions are the assertions which invokes when a method is invoked.
2.       Postconditions:  Postconditions are the assertions which invokes after a method finishes. 
Where to use Assertions?
We can use assertions in java to make it more understanding and user friendly, because assertions can be used while defining preconditions and post conditions of the program.

Example of AssertionExample.java

import java.util.Scanner;

public class AssertionExample
  {
 public static void main( String args[] )
  {
  Scanner scanner = new Scanner( System.in );

  System.out.print( "Enter a number between 0 and 20: " );
  int value = scanner.nextInt();
  assert( value >= 0 && value <= 20 ):
  "Invalid number: " + value;
  System.out.printf( "You have entered %d\n", value );
 }
  }

 Before running above example enable  the assertion at your environment with following command.
java -ea AssertionExample

What is Generics in java?




Arrays in Java have always been type safe. For example, an array declared as type integer can only accept integers (not String, Dog, Cat). But, collections are not like that. There is no syntax for declaring type safe collections before java 5. To create an Collection of a particular type java introduced Generics in java5.

ArrayList Without Generics

ArrayList lst = new ArrayList();

ArrayList With Generics

ArrayList<Integer>  lst = new ArrayList<Integer>();

Example Code:

import java.util.*;

public class demoCollection11
{
    public static void main(String s[])
    {
        System.out.println("ArrayList without Generics");
        ArrayList ar = new ArrayList();
        ar.add(10);
        ar.add(11);
        ar.add(12);
        Object a = ar.get(1);//Before generic it always return the object type
        System.out.println(ar);
        System.out.println("");
        System.out.println("ArrayList with Generics");
        ArrayList<String> ar1 = new ArrayList<String>();
        //ar1.add(10);   This will give error because ar1 list is of String type
        ar1.add("Ten");
        ar1.add("Eleven");
        ar1.add("Twelve");
       
        String str = ar1.get(1); //With generic, You can directly get the value in string type variable
        System.out.println(ar1);
    }
}

Output:

ArrayList without Generics
[10, 11, 12]

ArrayList with Generics
[Ten, Eleven, Twelve]

What is VarArgs or Variable length Arguments in java?

This is used when we don’t know how much argument should be passed to the Method but this is not a good practice in real time environment as it leads to maintenance problems. TO understand this more clearly please see the example below.

public class demoVarArgs
{
     void demoMethod(int a)
     {
         System.out.println("One argument demoMethod is called");
     }
     void demoMethod(int a,int b)
     {
         System.out.println("Two argument demoMethod is called");
     }
     void demoMethod(int...a)
     {
         System.out.println("Varible argument demoMethod is called");
     }

     public static void main(String s[])
     {
         demoVarArgs obj = new demoVarArgs();
         obj.demoMethod(1);
          obj.demoMethod(1,2);
           obj.demoMethod(1,2,3,4,5,6,7,8,9,10);
     }
}

Output:

One argument demoMethod is called
Two argument demoMethod is called
Varible argument demoMethod is called

In the above example there are three demoMethods with one ,two and variable number of arguments and we called the demoMethod by giving one, two and variable number of arguments. When it meets the exact number of argument defined in the method it called that particular method otherwise it always call method with variable number of arguments.