Notify
above snippet, I defined emp1 as of type Employee<String>, but I passed
employee id as 1 (1 is integer, not string). When you try to compile above
program, kotlin compiler throw below error.
ERROR: Type inference
failed. Expected type mismatch: inferred type is Employee<Int> but
Employee<String> was expected (9, 31)
2. Enable Programmers
to implement Generic Data Structures.
I
will explain about this in collections sections.
Is Kotlin had wildcard
characters like java?
No,
but kotlin has two other things declaration-site variance and type projections.
Kotlin:
Generics: Declaration-site variance : out
Kotlin
do not have wild card concept like Java, but it supports the same using Declaration-site variance and type projections
Why java support wild
cards?
?
is called the wild card in Java. It represents unknown type. There are some
situations, where programmer certainly don't know, the kind of data that he is
going to operate, in those situation wild cards are helpful.
First
let us see, why generic types are invariant in Java.
Why generic types are
invariant in Java?
Generic
types in Java are invariant. That means, if T2 is a subtype of T1,
List<T2> is not a sub type of List<T1>.
Ex
Integer
is a subtype of Number.
But
Stack<Integer> is not a sub type of Stack<Number>.
Why java makes generics
as invariant?
Let
me explain with an example.
/* Define a character list */
List<Character> chars =
new ArrayList<Character>();
/* Java do not allow this */
List<Object> objs =
chars;
/* Adding double to character
list */
objs.add(1.23);
/* ClassCastException: Cannot
cast double to Character */
Character s = chars.get(0);
As
you notify above snippet. I defined a character list ‘chars’ and cast it to
list of objects ‘objs’. I added a double to ‘objs’. Last statement cast the
double to character, which is not possible. To prohibit these kind of things
and ensure run time safety of the application, java generics are invariant.
But
generic type invariant has its own implications. For example, you would like to
calculate sum of numbers (number can be integer, byte, double, float etc.,).
Method signatures looks like below.
private static Double
sumOfintegers(List<Integer> listOfIntegers) {
}
private static Double
sumOfFloats(List<Float> listOfDoubles) {
}
private static Double
sumOfDoubles(List<Double> listOfDoubles) {
}
If
there are ‘n’ different Number types, you will end up in writing ‘n’ different
functions to calculate sum of the numbers. To resolve this kind of problem,
wild card character is introduced.
private static Double
sumOfNumbers(List<? extends Number> listOfNumbers) {
Double sum = 0.0;
for (Number num :
listOfNumbers) {
sum = sum +
num.doubleValue();
}
return sum;
}
The
wildcard type operator ‘? extends Number’ indicates that this method accepts a
collection of objects of number or some subtype of Numbers, not just E itself. We can use ‘sumOfNumbers’ function to
calculate the sum of different number types (Integer, Float, Double etc.,)
Sum
of integers can be calculated like below.
List<Integer>
intList = Arrays.asList(2, 3, 5, 7);
Double
sumOfIntegers = sumOfNumbers(intList);
Sum
of Doubles can be calculated like below.
List<Double>
doubleList = Arrays.asList(2.0, 3.0, 5.0, 7.0);
Double
sumOfDoubles = sumOfNumbers(doubleList);
Sum
of Floats can be calculated like below
List<Float>
floatList = Arrays.asList(2.0f, 3.0f, 5.0f, 7.0f);
Double
sumOfFloats = sumOfNumbers(floatList);
Find
the below working application.
Test.java
import java.util.Arrays;
import java.util.List;
public class Test {
private static Double sumOfNumbers(List<? extends Number> listOfNumbers) {
Double sum = 0.0;
for (Number num : listOfNumbers) {
sum = sum + num.doubleValue();
}
return sum;
}
public static void main(String args[]) throws CloneNotSupportedException {
List<Integer> intList = Arrays.asList(2, 3, 5, 7);
List<Double> doubleList = Arrays.asList(2.0, 3.0, 5.0, 7.0);
List<Float> floatList = Arrays.asList(2.0f, 3.0f, 5.0f, 7.0f);
Double sumOfIntegers = sumOfNumbers(intList);
Double sumOfDoubles = sumOfNumbers(doubleList);
Double sumOfFloats = sumOfNumbers(floatList);
System.out.println("sumOfIntegers : " + sumOfIntegers);
System.out.println("sumOfDoubles : " + sumOfDoubles);
System.out.println("sumOfFloats : " + sumOfFloats);
}
}