• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Usage of generics

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have just started peeping into jdk 1.5.

Generics were introduced so as to avoid the type casting for objects when retrieved from a collection. This feature is quite helpfull but i need some clarifications.
I have written a class as :

public class Test {
public static void main(String args[]) {
Test t = new Test();
ArrayList<String> a = new ArrayList();
a.add("a");
a.add("b");
a.add("c");
a.add("d");
a.add("e");
t.arrayListTest(a);
}
//******* generics in Collections **********//
public void arrayListTest(ArrayList a) {
for (int i=0; i < a.size();i++) {
String str1 = a.get(i);
System.out.println(str1);
}
}
}

When the java class is compiled,
I recieve an error saying "Cannot convert from Object to String"
at
String str1 = a.get(i);

I would like to know why is the generic type set for ArrayList a in main() is not applicable inside arrayListTest() method.

Regards
P. Vamsi Mohan
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vamsi Mohan Pemmaraju:
...I would like to know why is the generic type set for ArrayList a in main() is not applicable inside arrayListTest() method.


It's because the method arrayListTest takes an argument of type ArrayList. If you change the argument type to ArrayList<String>, then it will work.
 
Vamsi Mohan Pemmaraju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the diference between Arraylist<String> and ArrayList>?

Of course I understand that ArrayList<String> specifies that it holds objects of type String.

Any Implementation differences when using Generics??

Regards
P.Vamsi Mohan
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Arraylist without any type parameter acts as an plain old Collection's class,in which we can store different kind of objects.However to retrieve object from Arraylist you need to cast the object returned by get() function.This casting get resolved only at run time.
Try to run by casting the following snippets of code given above:
replace : -> String str1 = a.get(i);
with :-> String str1 = (String) a.get(i);

Now using this,we can get ClassCastException at run time(if user has kept different objects also in Arraylist and trying to get those objects).

To aviod runtime check ,we use Generics;
It allows us to declare the kind of objects we are going to store in Arraylist and thus ensures that compiler checks for any bugs at compile time only.
Try to parametrized
arrayListTest(ArrayList a) --> arrayListTest(ArrayList<String> a)
This restrict us to add only object of type String in Arraylist.

This will keep us out from casting the get() function and thus runtime checking.

Thanks
Mittu
 
I don't always make ads but when I do they're tiny
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic