Assumed generic types and flip side in JDK7

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<>(""); // JDK7 only
MyClass<Integer> myObject2 = new MyClass<>(""); // JDK7 only
MyClass<Integer> myObject3 = new <String> MyClass<Integer>("");
+3
source share
1 answer
MyClass<Integer> myObject = new <String> MyClass<>("");

simply

MyClass<Integer> myObject = new <String> MyClass<Integer>("");

1. MyClass <Integer> 2. String :

<String> MyClass(String t) {
    // ...
}

, " , ", , .

, , , , .:)

+2

All Articles