• 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

Different ways of Generic Declaration

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Just wanted to understand the different ways of collection declaration with/with out generics. (File name :P562_List_GenericDeclaration_Ask)

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:
List l2 = new ArrayList<String>(); //What is the use of this ?



Consider you're using an old Java library which doesn't use Generics.
Consider following class is from that library:


Now, if your new code uses String as generic parameter like this:


Then, the statement asked in the question is useful.

The variable lst2 is used just for the sake of explanation.
You can also call new GenDecl().funcName(lst); directly.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:
List<String> l3 = new ArrayList(); //What is the use of this ?



You can understand the answer to this question by going through following code:


But in this case, Java Compiler will give unchecked conversion warning, as you're trying to store List of anything in List of Strings.
 
reply
    Bookmark Topic Watch Topic
  • New Topic