Navigation
Recherche
|
Advanced programming with Java generics
jeudi 21 novembre 2024, 10:00 , par InfoWorld
Generics in Java enhance the type safety of your code and make it easier to read. In my last article, I introduced the general concepts of generics and showed examples from the Java Collections Framework. You also learned how to use generics to avoid runtime errors like the ClassCastException.
This article goes into more advanced concepts. I introduce sophisticated type constraints and operations that enhance type safety and flexibility, along with key concepts such as bounded type parameters, which restrict the types used with generics and wildcards and allow method parameters to accept varying types. You’ll also see examples of how to use type erasure for backward compatibility and enable generic methods for type inference. Generic type inference Type inference, introduced in Java 7, allows the Java compiler to automatically determine or infer the types of parameters and return types based on method arguments and the target type. This feature simplifies your code by reducing the verbosity of generics usage. When you use generics, you often specify the type inside angle brackets. For example, when creating a list of strings, you would specify the type as follows: List myList = new ArrayList(); However, with type inference, the Java compiler can infer the type of collection from the variable to which it is assigned. This allows you to write the above code more succinctly: List myList = new ArrayList(); With type inference, you don’t need to repeat String in the constructor of ArrayList. The compiler understands that since myList is a List, the ArrayList() must also be a String type. Type inference helps make your code cleaner and easier to read. It also reduces the chance of errors from specifying generic types, which makes working with generics easier. Type inference is particularly useful in complex operations involving generics nested within generics. Type inference became increasingly useful in Java 8 and later, where it extended to lambda expressions and method arguments. This allows for even more concise and readable code without losing the safety and benefits of generics. Bounded and unbounded type parameters In Java, you can use bounds to limit the types that a type parameter can accept. While bounded type parameters restrict the types that can be used with a generic class or method to those that fulfill certain conditions, unbounded type parameters offer broader flexibility by allowing any type to be used. Both are beneficial. Unbounded type parameters An unbounded type parameter has no explicit constraints placed on the type of object it can accept. It is simply declared with a type parameter, usually represented by single uppercase letters like E, T, K, or V. An unbounded type parameter can represent any non-primitive type (since Java generics do not support primitive types directly). Consider the generic Set interface from the Java Collections Framework: Set stringSet = new HashSet(); Set employeeSet = new HashSet(); Set customerSet = new HashSet(); Here, E represents an unbounded type parameter within the context of a Set. This means any class type can substitute E. Moreover, the specific type of E is determined at the time of instantiation, as seen in the examples where String, Employee, and Customer replace E. Characteristics and implications of unbounded type parameters Maximum flexibility: Unbounded generics are completely type-agnostic, meaning they can hold any type of object. They are ideal for collections or utilities that do not require specific operations dependent on the type, such as adding, removing, and accessing elements in a list or set. Type safety: Although an unbounded generic can hold any type, it still provides type safety compared to raw types like a plain List or Set without generics. For example, once you declare a Set, you can only add strings to this set, which prevents runtime type errors. Errors: Because the type of the elements is not guaranteed, operations that depend on specific methods of the elements are not possible without casting, which can lead to errors if not handled carefully. Bounded type parameters A bounded type parameter is a generic type parameter that specifies a boundary for the types it can accept. This is done using the extends keyword for classes and interfaces. This keyword effectively says that the type parameter must be a subtype of a specified class or interface. The following example demonstrates an effective use of a bounded type parameter: public class NumericOperations { // Generic method to calculate the square of a number public static double square(T number) { return number.doubleValue() * number.doubleValue(); } public static void main(String[] args) { System.out.println('Square of 5: ' + square(5)); System.out.println('Square of 7.5: ' + square(7.5)); } } Consider the elements in this code: Class definition: NumericOperations is a simple Java class containing a static method square. Generic method: square is a static method defined with a generic type T that is bounded by the Number class. This means T can be any class that extends Number (like Integer, Double, Float, and so on). Method operation: The method calculates the square of the given number by converting it to a double (using doubleValue()) and then multiplying it by itself. Usage in main method: The square method is called with different types of numeric values (int and double) that are autoboxed to Integer and Double, demonstrating its flexibility. When to use bounded type parameters Bounded-type parameters are particularly useful in several scenarios: Enhancing type safety: By restricting the types that can be used as arguments, you ensure the methods or classes only operate on types guaranteed to support the necessary operations or methods, thus preventing runtime errors. Writing reusable code: Bounded type parameters allow you to write more generalized yet safe code that can operate on a family of types. You can write a single method or class that works on any type that meets the bound condition. Implementing algorithms: For algorithms that only make sense for certain types (like numerical operations and comparison operations), bounded generics ensure the algorithm is not misused with incompatible types. A generic method with multiple bounds You’ve learned about bounded and unbounded type parameters, so now let’s look at an example. The following generic method requires its parameter to be both a certain type of animal and capable of performing specific actions. In the example below, we want to ensure that any type passed to our generic method is a subclass of Animal and implements the Walker interface. class Animal { void eat() { System.out.println('Eating...'); } } interface Walker { void walk(); } class Dog extends Animal implements Walker { @Override public void walk() { System.out.println('Dog walking...'); } } class Environment { public static void simulate(T creature) { creature.eat(); creature.walk(); } public static void main(String[] args) { Dog myDog = new Dog(); simulate(myDog); } } Consider the elements in this code: Animal class: This is the class bound: The type parameter must be a subclass of Animal. Walker interface: This is the interface bound: The type parameter must also implement the Walker interface. Dog class: This class qualifies as it extends Animal and implements Walker. Simulate method: This generic method in the Environment class accepts a generic parameter, T, that extends Animal and implements Walker. Using multiple bounds (T extends Animal and Walker) ensures the simulated method can work with any animal that walks. In this example, we see how bounded generic types leverage polymorphism and ensure type safety. Wildcards in generics In Java, a wildcard generic type is represented with the question mark (?) symbol and used to denote an unknown type. Wildcards are particularly useful when writing methods that operate on objects of generic classes. Still, you don’t need to specify or care about the exact object type. When to use wildcards in Java Wildcards are used when the exact type of the collection elements is unknown or when the method needs to be generalized to handle multiple object types. Wildcards add flexibility to method parameters, allowing a method to operate on collections of various types. Wildcards are particularly useful for creating methods that are more adaptable and reusable across different types of collections. A wildcard allows a method to accept collections of any type, reducing the need for multiple method implementations or excessive method overloading based on different collection types. As an example, consider a simple method, displayList, designed to print elements from any type of List: import java.util.List; public class Demo { public static void main(String[] args) { List colors = List.of('Red', 'Blue', 'Green', 'Yellow'); List numbers = List.of(10, 20, 30); displayList(colors); displayList(numbers); } static void displayList(List list) { for (Object item: list) { System.out.print(item + ' '); // Print each item on the same line separated by a space } System.out.println(); // Print a newline after each list } } The output from this method would be: Red Blue Green Yellow 10 20 30 Consider the elements of this code: List creation: Lists are created using List.of(), which provides a concise and immutable way to initialize the list with predefined elements. displayList method: This method accepts a list with elements of any type (List). Using a wildcard (?) allows the method to handle lists containing objects of any type. This output confirms that the displayList method effectively prints elements from both string and integer lists, showcasing the versatility of wildcards. Lower-bound wildcards To declare a lower-bound wildcard, you use the? super T syntax, where T is the type that serves as the lower bound. This means that the generic type can accept T or any of its superclasses (including Object, the superclass of all classes). Let’s consider a method that processes a list by adding elements. The method should accept lists of a given type or any of its superclasses. Here’s how you might define such a method: public void addElements(List
https://www.infoworld.com/article/3595651/advanced-programming-with-java-generics.html
Voir aussi |
56 sources (32 en français)
Date Actuelle
dim. 22 déc. - 13:40 CET
|