Hi all
I am trying to implement generics concept in jdk1.5.
Please have a glance at the below code:
import java.util.*;
class GenericsDemoo
{
public static void main(
String args[])
{
ArrayList<String> al = new ArrayList<String>();
al.add("Naresh");
al.add("Kumar");
al.add("Sunil");
al.add("Santosh");
Iterator it = al.iterator();
while(it.hasNext())
{
String str=it.next();
System.out.println(str.substring(0,2));
}
}
}
When i tried to compile the above code i am facing a compile time error with the message.
GenericsDemoo.java:14: incompatible types
found : java.lang.Object
required : java.lang.String
String str=it.next();
1 error MyQuestion: The only concept behind generics is we need not do the typecasting for the objects retrieved from the collection.Isn't it? or did i understood generics concept wrongly.please clarify me.
Thanks in advance.