• 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 q

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

then what is the difference between
1. Basket b1 = new Basket<Fruit>();
2. Basket<Fruit> b2 = new Basket<Fruit>();
PS: Both compiles fine.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b1.setElement(new String("Test")); // OK,
b2.setElement(new String("Test")); //Compile-time error
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Basket b1 = new Basket<Fruit>(); // Tack any tupe of Object as input
2. Basket<Fruit> b2 = new Basket<Fruit>(); //Tack only Fruit Object as input
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pinkal Patel:
1. Basket b1 = new Basket<Fruit>(); // Tack any tupe of Object as input
2. Basket<Fruit> b2 = new Basket<Fruit>(); //Tack only Fruit Object as input



The primary benefit of generics is to provide compile time checking (strong typing) for collection elements. Your example reinforces this point.
 
reply
    Bookmark Topic Watch Topic
  • New Topic