• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main(String[] args)
{
Queue<String> q=new LinkedList<String>();
q.add("Abhishek");
q.add("Duncan");
showAll(q);
}
public static void showAll(Queue q)
{
q.add(new Integer(42));
while(!q.isEmpty())
System.out.print(q.remove()+"");
}

here is my doubt I declared 'q' is of type Queue which is generic type and can hold only String values, whereas in showAll() wch takes queue as parameter, iam trying to enter interger objects into the queue. Is it possible if yes explain me..
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in method showall q is not generic
if you want to make it generic use Queue<String> q
as shown below.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhishek Reddy Chepyala:
here is my doubt I declared 'q' is of type Queue which is generic type and can hold only String values, whereas in showAll() wch takes queue as parameter, iam trying to enter interger objects into the queue. Is it possible if yes explain me..



Yes, this works, since the Generic code can be freely mixed with corresponding non-generic code. Howeverif you try to treat the Integer as a String later on [assuming that you passed a Queue<String> ;] , you would get a ClassCastException.
[ August 07, 2006: Message edited by: Neelesh Bodas ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the compiler will warn you in line


The compiler warning occurs because changing members of generic classes that are used non-generically is not type safe.
showAll takes raw type Queues as parameter, and these operate on type Object, so you can add anything to it (Integer, Strings, JButtons...).
The warning makes sense here, as your code would produce a Class Cast Exception at runtime. But also if you used add("a string"); the warning would have been there.
If you used showAll generically <String>, adding an Integer would shift from an exception at runtime to a compile time error. So Generics helps you to see your programming mistakes earlier - but only if you use generics.

Yours,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic