Featured Post

Generics and Collections in Java 1.5 (continue..)

In the Last Post we discussed about Collections classes not more about generics . But this Post is concentrating on Generics . Generics in java has been introduced in 1.5 version . This time learners will feel that Learning Java is not So Easy . Sun developers are really innovative and Talented hence they have come up with generics and solving the issues.

In java Generics have been introduced to resolve the collection problems like adding any element to collections and while retrieveing the object need to type cast and get the Object . But this is really flaw in the code ,Since adding a some other class in that collection object will give cast exception which is really bad . Hence Generics has been introduced in Java 1.5 here you can say what is your collection object contains .Check the below code which is declration of Generics in java 1.5

List l=new ArrayList();

This List object will accept only the Integers as collection object .

Here We need to discuss about AutoBoxing and Unboxing

AutoBoxing
**********
Normally We need to add objects in the collections but check below code

l.add(1); here we are adding the 1 primitive value is directly added to the list object which will beinternally converted into Integer Object since the collection knows which type of object will it accept .


Unboxing
********
This is reverse process of retrieveing the object from that collection . Here No need to gwrite old code

1.4 code
********

Integer ii=(Integer)l.get(1);
ii.intVlaue() ;//old code


1.5 code
********

int i=l.get(1); // unboxing


Arrays difference with Collections
**********************************

Object c[]=new Integer[100]; this will work

But generics subtypes will not work
check the below code

List obj=new ArrayList(); // wrong code
It gives compilation error
Since Generics subtype will not be valid . you can not add different type objects to reference type collection which is of type super

But you can add collection of type Numbers can add Integer Objects
check the below code

List num=new ArrayList();
num.add(new Integer("2")); // this is valid
Since you are adding sub type object which works as like above arrays object code .


Some more Points about Generics do's and dont's
***********************************************
1)List ll=new ArrayList() will not compile since you are using wild card in object type .
2) Means above point says Object creation you can not use ? wild card character in generics usage
3) List you can use this at reference type but the thing is you can not add
any thing to this collection ex as folowes

public void doThis(List list)
{
list.add(new Integer("2000"));// will not compile
// since here you are telling to compiler that you are not adding any objects simply //doing some other operations
}

4) List can add any objects to this list .



public void doAny(List list)
{
list.add(new Integer("2000"));// this compiles
//this is valid since you are telling compiler any object of super of integer can be added //to this list . So you can do any operations .
}


5)generic classes will not allow wild card characters
like check the below code

public class NewTemplate
{} //valid code compiles
public class NewTemplate
{} // valid code compiles

But below code will not be compiled

public class newTemplate{
} // this code will not be compiled . since you have used ? wild card character


Simple class Generic Template
*****************************

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mythreadtest;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author Venkat
*/
public class MyNewTemplate {

T1 t1;
T2 t2;
public MyNewTemplate(T1 t1,T2 t2)
{
this.t1=t1;
this.t2=t2;

System.out.print("Constructor MyNewTemplate Is called");

}


public T1 getObject1()
{
return t1 ;
}

public T2 getObject2()
{
return t2 ;
}

public static void main(String args[])
{
MyNewTemplate s=new MyNewTemplate(new Integer("10"),"ABCD");
System.out.println(s.getObject1());
System.out.println(s.getObject2());

}

}



You can use Type parameter without making class as a generic class like as follows

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mythreadtest;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author Venkat
*/
public class MyNewTemplate{


public MyNewTemplate(T1 t1,T2 t2)
{

System.out.print("Constructor MyNewTemplate Is called");

}


public T1 getObject1(T1 t1)
{
return t1;
}

public T2 getObject2(T2 t2)
{
return t2 ;
}

public static void main(String args[])
{
MyNewTemplate s=new MyNewTemplate(new Integer("10"),"ABCD");
System.out.println(s.getObject1("11"));
System.out.println(s.getObject2("ABCD"));

}

}


Note : here Class is not generic Type class but those constructors and methods are generic type
to define generic type parameter you have to use infront of contructor to use T1 and T2 in the constructor parameters
even infron of methods also you have to define so that you can use that generic type as parameter in the method


Note : you can use class name and type parameter name as same . there will not be any conflicts
check the below code

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mythreadtest;

/**
*
* @author Venkat
*/
public class X {

public X(X x)
{
System.out.println(x.toString());
}


public X getX(X x)
{
return x;
}


public static void main(String args[])
{
X x=new X(new Integer("2"));
X x1=new X(new String("AB"));
}

}


Note:
*******
There will not be any conflict between type parameter and normal class in the above example even if they are same




Important Points
****************

List list=new ArrayList() // will accept only integers
List list=new ArrayList() // will accept any thing




Please give me suggestions and comments on this you could reach me at srampv@gmail.com


Thanks
Venkat



Enter you DOB ,Time and Place

SEO Schemes

Comments

Popular posts from this blog

[Inside AdSense] Understanding your eCPM (effective cost per thousand impress...