• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics  RSS feed

 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what kind of coverage is given to generics in this book? Are they similar to C++ templates?

thanks,
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not Brett,but ...

Chapter 2 (20 pages) is all about generics. After that the authors use generics in their examples on other topics. So generics gets excellent coverage.

Generics are similar in concept to C++ templates except that you don't need a separate one for each type. For example, List<Integer> and List<String> aren't compiled into two classes the way they are in C++.
 
Brett McLaughlin
author
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics get pretty prominent coverage in the Tiger notebook. Although there is technically only a chapter on the subject (Chapter 2), there are a number of chapters that play a part -- for example, varargs have a lot to do with generics, and they get a chapter as well (Chapter 5).

In a nutshell, generics are more about type-safety and parameterization than they are templates, although the end-result is very similar. So you can, as the previous poster pointed out, do something like:

List<String> stringList = new LinkedList<String>;

That means that, at compile time, javac ensures that only Strings are added to stringList. However, at runtime, you can get around these things because of erasure (I just posted some things on that here: http://www.coderanch.com/t/374888/java/java/Reflection-jdk)

Let me know what you want to drill into -- I'm trying to do overview answers, and let the folks on the forum direct more specific discussion.

Thanks
Brett
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!