|
|
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
Page 9 of 9
27
43
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).
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:
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).
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.
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.
Read more about Core Java in JavaWorld's Core Java section.
java.util.concurrent.
java.time classes you're most likely to need.