While following a course for
Java 8
Generics
Lambda expressions
Generic
String
public static String betterString(String s1,String s2,IBetterString bs){
if(bs.isBetter(s1, s2)){
return s1;
}else{
return s2;
}
}
public static <T> betterEllement(T s1, T s2, BiPredicate<T, T> bi) {
if (bi.test(s1, s2)) {
return s1;
} else {
return s2;
}
}
Return type
<T>
Because you return nothing using your method. <T>
define it's a generic Type which is not known yet. That doesnt mean that you return it. You need to add another T which is the generic Type return value.
Its
public static <T> T betterEllement(T s1, T s2, BiPredicate<T, T> bi) {
if (bi.test(s1, s2)) {
return s1;
} else {
return s2;
}
}
<T>
can also be used if you know the return value but want to define generic arguments.
public static <T> String getStringTest(T test) {
if (test.getClass().isInstance(WhatEver.class)) { ....}
return "test";
}
You can see it perfectly using a generic Class.
class TestGeneric<T> {
public T betterElement(T s1) { /** .. **/ return null; }
}