Posts

Showing posts from June, 2018

Java Generics - 5. Wildcards

Image
This is part 5 of the 9 part series on Java Generics  Prev Topic Generic Methods Topics 1. Introduction 2. Bounding 3. Multiple bounding 4. Generic Methods 5. Wildcards 6. Typed Classes as method parameters 7. Upper and Lower Bounds 8. Target Types 9. Type Erasure Next Topic Typed Classes as method parameters Java Generics - Wildcards “?” wildcard in Java, means “any”. If a method takes in a declaration with <?>, it means it can take any type. Say you’d like to perform a certain operation which applies to all types (usually for a library class) , then this wildcard can be handy. Say you want a method which gives back the size of the list, but you also want to handle ‘null’ scenarios. Then you could write the code as:        public static int getSize(List<?> list ){               re...