• 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 unknown type - <?>

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What type if any can be added to a collection if a collection is declared as



I know that <?> means "any type". I tested it and you cant add any type and i can see why that is the case.
I dont understand the instance creation type. What is the significance of it being a String instance? Does it make any difference? ( i tried adding a string object to the collection and the compiler didnt like it)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
I know that <?> means "any type". I tested it and you cant add any type and i can see why that is the case.



To be exact, it actually means that the compiler has no idea what type it is. The collection takes a very specific type (not any type), it is just that the compiler doesn't know what type it takes.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
I dont understand the instance creation type. What is the significance of it being a String instance? Does it make any difference? ( i tried adding a string object to the collection and the compiler didnt like it)



Quite frankly. Don't do this.

It makes no sense to create a collection of a particular type, and then tell the compiler to forget about the type -- on the same line.

Wildcards are more commonly used for parameters, where you want to write a method that can work with multiple collection types. And where you don't care about the type.... for example, a method that iterates through the collection, and prints out the output from the toString() method. Without wildcards, you would probably have to disable generics altogether to write a generic printing method.

Henry

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Quite frankly. Don't do this.



But we're in one of the certification forums. Most of the code I see posted here is of the "don't do this" variety. In fact you'd almost think that the certification exams were designed to teach people bad ways to write programs.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Henry Wong wrote:
Quite frankly. Don't do this.



But we're in one of the certification forums. Most of the code I see posted here is of the "don't do this" variety. In fact you'd almost think that the certification exams were designed to teach people bad ways to write programs.




Sad, but true ...

Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw that example on one of Suns's online tutorials. See http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf (Last code snippet on page 5).

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic