Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Java Generics v1.5

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone have a good example using Generics? I've read a couple of articles about the use of generics, but I haven't really found any great examples.

Thanks in advance-

Josh
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I bet Herb's Book has some good examples. There is also a nice simple example showing the difference between 1.4 and 5 at the very beginning of this thread.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the other threads in this forum. Generics have been discussed and argued about in great detail over the past two days. The other threads on this topic have examples and links to more.

HTH

Layne
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Josh:

First, my book has in-depth coverage of generics. In fact, the generics chapter is nearly 50 pages long. There is a lot to generics, far more than one or two examples can show.

That said, here is a very simple example that shows how to create a generic class and create an object of that class. Pay close attention to the new syntax elements.


The output produced is shown here.

value: 88

value: Generics Test

The key point is that the Gen class is generic. Its type parameter is T, which is a placeholder for the real type that will be known when an object of type Gen is instantiated. Thus, Gen<Integer> creates an Gen reference in which Integer is substituted for T. Gen<String> creates a reference in which String is substituted for T. (Actually, the process used by the Java generics system is type erasure, not substitution, but conceptually that is how it appears to the programmer.) Most importantly, these types are enforced by the compiler.

For example, assuming the preceding program, this line will not compile because of a type mismatch error.

In the program, iOb is of type Gen<Integer>. Because of this, it can't be assigned a reference to a Double object. This is a key feature of generics: type safety.

Josh, frankly, there is a whole lot more to generics than this simple example shows, but it gives you a look at the basic syntax.

Here are other important features of generics: bound types, wildcards, raw types, erasure, bridge methods, and potential ambiguity.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic