Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java 101: The next generation: The essential Java language features tour, Part 1

Java programming with assertions and generics

  • Print
  • Feedback

Page 9 of 9

27
43

What's controversial about generics in the Java language?

While generics as such might not be controversial, their particular implementation in the Java language has been. Generics were implemented as a compile-time feature that amounts to syntactic sugar for eliminating casts. The compiler throws away a generic type or generic method's formal type parameter list after compiling the source code. This "throwing away" behavior is known as erasure. Other examples of erasure in generics include inserting casts to the appropriate types when code isn't type correct, and replacing type parameters by their upper bounds (such as Object).

More generics controversy

Generics are controversial for reasons other than erasure. See the StackOverflow.com topic "Why do some claim that Java's implementation of generics is bad?" for more discussion, including the suggestion that wildcards can be confusing and the fact that generics do not support value types (for instance, you cannot specify List<int>).

Using erasure leads to several limitations:

  • With one exception, the instanceof operator cannot be used with parameterized types. The exception is an unbounded wildcard. For example, you cannot specify Set<Shape> shapes = null; if (shapes instanceof ArrayList<Shape>) {}. Instead, you need to change the instanceof expression to shapes instanceof ArrayList<?>, which demonstrates an unbounded wildcard. Alternatively, you could specify shapes instanceof ArrayList, which demonstrates a raw type (and which is the preferred use).
  • The compiler transforms generic code into non-generic code, which is stored in the classfile. Some developers have pointed out that erasure prevents you from using reflection to obtain generics information, which isn't present in the classfile. In "Java Reflection: Generics" developer Jakob Jenkov points out a few cases where generics information is stored in a classfile, and this information can be accessed reflectively.
  • You cannot use type parameters in array-creation expressions; for example elements = new E[size];. The compiler will report a generic array creation error message if you try to do so.

Given erasure's limitations, you might wonder why generics were implemented with erasure. The reason is simple: the Java compiler was refactored to use erasure so that generic code could interoperate with legacy Java code, which isn't generic. Without that backward compatibility, legacy Java code would fail to compile in a Java compiler supporting generics.

Conclusion to Part 1

The Java language has evolved through the addition of many new features. In this article I've shown you how to use assertions to increase your confidence in the correctness of your code, and how to use generics to eliminate ClassCastExceptions. By using assertions and generics, you can craft code that's much more reliable and minimize code failure at runtime, as well as the associated headache of dealing with angry clients.

Java 5 was a pivotal release in the history of the Java platform, and while generics were more controversial than some other features, they weren't necessarily more important. My next article will introduce seven more essential features added to the language in Java 5: typesafe enums, annotations, autoboxing and unboxing, enhanced for loops, static imports, varargs, and covariant return types. Until then, check out the source code for this article, which contains more tips and examples for programming with assertions and generics.

About the author

Jeff Friesen is a freelance tutor and software developer with an emphasis on Java and Android. In addition to writing Java and Android books for Apress, Jeff has written numerous articles on Java and other technologies for JavaWorld, informIT, Java.net, DevSource, and SitePoint. Jeff can be contacted via his website at TutorTutor.ca.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources
  • Download the source code for this article.
  • Read Angelika Langer's Java Generics FAQs for a wealth of information and perspective about generics in the Java language.
  • For students of the Java language and its controversies, Langer's "Und erstanding the closures debate" (JavaWorld, June 2008) compares the three initial proposals for adding closures, or lambda expressions, to the Java language in Java 7.
  • See "Java Reflection: Generics" (Jakob Jenkov, Jenkov.com) for further discussion about reflection with generics and special cases where it is possible to access generics information at runtime.
  • More from Java 101: The next generation:
  • More about the Java Collections Framework on JavaWorld: