Do you know Java: PECS
Do you remember the rule of PECS? If not, read the post quickly!
PECS stands for Produce-Extend, Consume-Super. What does that mean? Let us see an example with types from the standard.
Object
void add(List<Object> list) {
list.add("string");
list.add(1);
}
Passing the followings are:
add(new ArrayList<Object>()); // not type safe
add(new ArrayList<String>()); // compile time error
The first is not type safe, the second one does not compile, since ArrayList<String> is not the subtype of List<Object>.
Wildcard
Wildcard ? brings the following.
void add(List<?> list) {
list.add("string"); // compile time error
list.add(1); // compile time error
}
Though, add accepts both ArrayList<String> and ArrayList<Integer>, but not known what kind of parameter is passed during runtime,
so adding new elements to the list (writing the list) is not allowed.
Super (Lower bounded wildcard)
void add(List<? super Number> list) {
list.add(1);
for (Object number : list) {
System.out.println(number);
}
}
? super Taccepts all types betweenTandObjecton the hierarchy chain.listis writable, all values are accepted whereinstanceof Numberreturnstrue.listis considered non-readable, except the read value isObjectbecause of type erasure.- We say that with super, new item is consumed by the list.
add(new ArrayList<String>()); // compile time error
add(new ArrayList<Integer>()); // compile time error
add(new ArrayList<>());
add(new ArrayList<Object>());
Extends (Upper bounded wildcard)
void add(List<? extends Number> list) {
list.add(1); // compile time error
for (Object number : list) {
System.out.println(number);
}
}
? extends Taccepts all types that extendsNumber, likeFloat,Double,Integer, andNumber.listis not writale, sinceObjectis the only type which is known in runtime.listis readable, and the type of the return value can be anything betweenNumberandObject.- We say that with extends, new item is produced from the list.
add(new ArrayList<String>());
add(new ArrayList<Integer>()); // compile time error
add(new ArrayList<>());
add(new ArrayList<Object>()); // compile time error
In this post we see how to use generic boundaries and what PECS is.
Code can be found: https://github.com/torokmark/do-you-know-java