Monday, July 25, 2016

How to store a default values in a List for a specific size


We can store a default values to the List interfcae implementation classes like ArrayList, LinkedList etc .
The storing a specific default value to the specific size of the array List implementation class is possible, Please check the below code for it.

Storing Integer values 0 as default value to the ArrayList of size 100;
      Integer intArray[] = new Integer[100];
      List al = new ArrayList(Arrays.asList(intArray));
      Collections.fill(al,0);
      System.out.println("ArrayList values are :"+al);
Storing String values "zameer" as default value to the ArrayList of size 100;
      String stArray[] = new String[100];
      List al = new ArrayList(Arrays.asList(stArray));
      Collections.fill(al,"zameer");
      System.out.println("ArrayList values are :"+al);
Please give us your comments on this article.

Monday, July 18, 2016

How to know the data type of the elements from the list of collection objects in java

To know the data types from the list of collection objects in java is by using the below code


public class CheckDataTypes{

      pubic static void main(String[] args){
   
   List list = new ArrayList();
list.add(10);
list.add("hello");
list.add('a');
list.add(34.4);
list.add(22.3f);
for(Object obj:list){
if(obj.getClass().equals(String.class)){
System.out.println(" String type");
}else if(obj.getClass().equals(Integer.class)){
System.out.println("Integer type");
}else if(obj.getClass().equals(Float.class)){
System.out.println("Float type ");
}else if(obj.getClass().equals(Double.class)){
System.out.println("Double type ");
}else if(obj.getClass().equals(Character.class)){
System.out.println("Char Type");
}
}
      }//Main method closed

}// class ended



One more way is : 


public class DataType {
   public static void main(String args[]){
     
  List list = new ArrayList();
list.add(10);
list.add("hello");
list.add('a');
list.add(34.4);
list.add(22.3f);
for(Object obj:list){
if(obj instanceof String){
System.out.println(" String type");
}else if(obj instanceof Integer){
System.out.println("Integer type");
}else if(obj instanceof  Float){
System.out.println("Float type ");
}else if(obj instanceof  Double){
System.out.println("Double type ");
}else if(obj instanceof Character){
System.out.println("Char Type");
}
    }//for each loop closed
   }//main method closed
}//Class is closed

Is this variable declaration is valid?

       
In the below different variable declaration is valid.

Class Test{


  1.         int a=0.0;  
  2. Integer b=23.4;
  3. String s='d';
  4. char c="sample";
  5. char ch[]="simple data";
  6. float f=34.5;
  7. double d=f;
  8. long l=d;

}

1.Compilation error. Error : Type mismatch: cannot convert from double to int
2.Compilation error: Error:  Type mismatch: cannot convert from double to Integer
3.Compilation error: Error:  Type mismatch: cannot convert from char to String
4.Compilation error: Error:  Type mismatch: cannot convert from String to char
5.Compilation error: Error:  Type mismatch: cannot convert from String to Char Array
6.Compilation error: Error:  Type mismatch: cannot convert from double to float
8.Compilation error: Error:  Type mismatch: cannot convert from double to long

Saturday, July 9, 2016

What is request dispatcher and how many ways we can create an dispatcher object

Request Dispatcher :


Request Dispatcher is an interface which is having two methods forward(request, response) and include(request,response).

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.

This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.

Simple explanation of request dispatcher is to forward control(request) to the next resource (like jsp,servlet or html) which has to do the processing of the request  and it will return the result to the end user or it can return the processed request to the calling function to fulfill the remaining processing of the request then the calling function will return the result to the end user.

We can create Request dispatcher in two ways

ServletContext context = getServletContext();
                 RequestDispatcher dispatcher = context.getRequestDispatcher("/processing.jsp");
                 dispatcher.forward(request,response);
RequestDispatcher requestDispatcher; 
requestDispatcher = request.getRequestDispatcher("/thankYou.jsp");   
requestDispatcher.forward(request, response); 
requestDispatcher.include(request,response);