I go through the Java Tutorial and have been reading about a typical type in JDK7 .
I came across the following syntax ...
class MyClass<X> {
<T> MyClass(T t) {
}
}
MyClass<Integer> myObject = new <String`> MyClass<>("");
... which is a bit confusing. I understand the diamond operator and how you can define generic types based on context.
I'm not sure why you used the diamond operator to deduce the type passed to the constructor while at the same time explicitly specifying the type "String"? I also do not understand why the reverse is involved!
Also, is there a difference between the following?
MyClass<Integer> myObject1 = new <String> MyClass<>("");
MyClass<Integer> myObject2 = new MyClass<>("");
MyClass<Integer> myObject3 = new <String> MyClass<Integer>("");
source
share