Home arrow Java arrow Page 5 - Wildcards, Arrays, and Generics in Java
JAVA

Wildcards, Arrays, and Generics in Java


In this conclusion to a five-part series, you will learn about wildcards, arrays, and more. This article was excerpted from chapter eight of the book Learning Java, third edition, written by Patrick Niemeyer and Jonathan Knudsen (O'Reilly; ISBN: 0596008732). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
June 07, 2007
TABLE OF CONTENTS:
  1. · Wildcards, Arrays, and Generics in Java
  2. · Wildcard Types Versus Generic Methods
  3. · Using Array Types
  4. · Case Study: The Enum Class
  5. · Case Study: The sort() Method

print this article
SEARCH DEVARTICLES

Wildcards, Arrays, and Generics in Java - Case Study: The sort() Method
(Page 5 of 5 )

Poking around in the java.util.Collections class we find all kinds of static utility methods for working with collections. Among them is this goody--the static generic method sort() :

  <T extends Comparable<? super T>> void sort( List<T> list ) { ... }

Another nut for us to crack. Let's focus on the last part of the bound:

  Comparable<? super T>

This is a wildcard instantiation of the Comparable interface, so we can read the extends as implements, if it helps. Comparable holds a compareTo() method for some parameter type. A Comparable<String> means that the compareTo() method takes type String. Therefore, Comparable<? super T> is the set of instantiations of Comparable on T and all of its superclasses. A Comparable<T> suffices and, at the other end, so does a Comparable<Object>. What this means in English is that the elements must be comparable to their own type or some supertype of their own type. This is sufficient to ensure that the elements can all be compared to one another, but not as restrictive as saying that they must all implement the compareTo() method themselves. Some of the elements may inherit the Comparable interface from a parent class that knows how to compare only to a supertype of T and that is exactly what is allowed here.

Conclusion

Java generics are a very powerful and useful addition to the language. Although some of the details we delved into later in this chapter may seem daunting, the common usage is very simple and compelling: generics make collections better. As you begin to write more code using generics you will find that your code becomes more readable and more understandable. Generics make explicit what previously had to be inferred from usage. They complete the promise of type safety in the Java language and make Java a better language, despite their idiosyncrasies.  


* That is, unless you want to use a generic type in a nongeneric way. We’ll talk about “raw” types later in thischapter.

* For those of you who might like some context for the title of this section, here is where it comes from:

Boy: Do not try and bend the spoon. That’s impossible. Instead only try to realize the truth.

Neo: What truth?

Boy: There is no spoon.

Neo: There is no spoon?

Boy: Then you’ll see that it is not the spoon that bends, it is only yourself.

—Wachowski, Andy and Larry. The Matrix. 1 videocassette (136 minutes). Warner Brothers, 1999.

* In real life, Java doesn’t let us extend the Enum type; that’s reserved for the enum keyword and the compiler. But the structure is as shown.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials