• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Generics

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic