Friday, January 29, 2010

Generic type warnings

Unless you've successfully completed the extra credit with generic types, you're getting compile-time warnings about type safety. If you use the -Xlint:unchecked compiler flag (see an earlier blog post), you'll get details about the warning but not much insight into why it's happening or how to fix it.

Why it's happening: The compiler can be sure that both target and a.get(i) on line 11 are both instances whose classes implement the Comparable interface, but there's no guarantee that they are mutually comparable. For example, target could be a Photo and a.get(i) could be a String. Both are Comparables, but they can't be compared to each other. So, the compiler gives you a warning.

What to do about it: Nothing. Ignore it. Unless you're trying to earn extra credit for making the SearchLibrary class generic, don't worry about these warnings. They're just warnings, after all, and they won't prevent your program from compiling and running. If you're trying for the extra credit, however, a successful generic implementation of SearchLibrary will eliminate the warnings (and earn you 10 extra points).

No comments: