No need to resort to type - one of the biggest advantages of Java generics , as it will perform type checking at compile time. This will reduce the possibility of a ClassCastException that can be ClassCastException at runtime, and can lead to more robust code.
But I suspect that you are fully aware of this.
Every time I look at Generics, it gives me a headache. I find the best part of Java is simplicity and minimal syntax and generics are not simple and add a significant amount of new syntax.
Firstly, I also did not see the benefits of generics. I started learning Java from syntax 1.4 (although Java 5 was not available at the time), and when I came across generics, I felt like writing more code, and I really didn't understand the benefits.
Modern IDEs make it easy to write code with generics.
Most modern, decent IDEs are smart enough to help you write code with generics, especially with code completion.
Here is an example of creating a Map<String, Integer> with a HashMap . The code I would have to enter:
Map<String, Integer> m = new HashMap<String, Integer>();
And indeed, it is a lot to print just to create a new HashMap . However, in fact, I only had to type this information before Eclipse knew what I needed:
Map<String, Integer> m = new Ha Ctrl + Space
True, I needed to select a HashMap from the list of candidates, but basically the IDE knew what to add, including generic types. Using the right tools, using generics is not so bad.
In addition, since the types are known, when extracting elements from the general collection, the IDE will act as if this object is already an object of its declared type - there is no need for a cast for the IDE to know what the type of the object is.
A key advantage of generics is how it works well with the new features of Java 5 . Here is an example of selecting integers in Set and calculating its total amount:
Set<Integer> set = new HashSet<Integer>(); set.add(10); set.add(42); int total = 0; for (int i : set) { total += i; }
There are three new Java 5 features in this code snippet:
First, generics and autoboxing primitives allow the following lines:
set.add(10); set.add(42);
The integer 10 is autoboxed in Integer with a value of 10 . (And the same for 42 ). Then, that Integer rushes into Set , which, as you know, holds Integer s. Attempting to throw String will result in a compilation error.
Further, for each cycle, all three of them:
for (int i : set) { total += i; }
First, a Set containing an Integer is used in every loop. Each element is declared as int , and this is allowed, since Integer unpacked back to the int primitive. And the fact that this unlocking is happening is known because generics were used to indicate that Set had Integer .
Generics can be a glue that combines the new features introduced in Java 5, and just simplifies and simplifies coding. And in most cases, the IDEs are smart enough to help you with good suggestions, so as a rule, this will not print much more.
And frankly, as the Set example shows, I believe that using Java 5 features can make the code more concise and reliable.
Edit - an example without generics
The following is an example of the Set example above without using generics. It is possible, but not entirely pleasant:
Set set = new HashSet(); set.add(10); set.add(42); int total = 0; for (Object o : set) { total += (Integer)o; }
(Note: The above code will generate a warning without warning at compile time.)
When using non-generics collections, the types that are introduced into the collection are objects of type Object . Therefore, in this example, a Object is what is included in the set in add ed.
set.add(10); set.add(42);
In the above lines, autoboxing is in the game - the primitive value is int 10 and 42 autoboxing in Integer objects that are added to Set . However, keep in mind that Integer objects are treated as Object s, as there is no type information to help the compiler know what type Set should expect.
for (Object o : set) {
This is an integral part. The reason every loop works is because Set implements an Iterable interface that returns an Iterator with type information, if any. ( Iterator<T> , that is.)
However, since there is no type information, Set will return an Iterator , which will return values to Set as Object s, and that is why the element that is retrieved in for- each loop must be of type Object .
Now that the Object is retrieved from Set , it must be completed manually for Integer to complete the addition:
total += (Integer)o;
Here, type-type runs from Object to Integer . In this case, we know that this will always work, but manual type casting always makes me feel like it is fragile code that can be damaged if minor changes occur otherwise. (I feel that every arrival like ClassCastException waiting, but I digress).
Integer now unpacked into int and allowed to add int total to the variable.
I hope I could illustrate that the new features of Java 5 can be used with non-standard code, but it's just not as clean and straightforward as writing code with generics. And, in my opinion, in order to take full advantage of the new features in Java 5, you need to look at generics if, at least, it allows compile-time checks to prevent invalid type casts to throw runtime exceptions.