First look at the error code:
Public class Holder <T> {
Private T value;
Public Holder (){
}
Public Holder (T value ){
This. value = value;
}
Public void setValue (T value ){
This. value = value;
}
// Some rows are omitted here.
}
Holder <Object> holder = new Holder <> ("xxx ");
It looks okay, but an error is reported during compilation:
Uncompilable source code-incompatible types
Required: javax. xml. ws. Holder <java. lang. Object>
Found: javax. xml. ws. Holder <java. lang. String>
It is okay to write the type honestly:
Holder <Object> holder = new Holder <Object> ("xxx ");
If you must use the diamond operator, you can use either of the following methods:
// Use the default constructor and call the setValue method.
Holder <Object> holder = new Holder <> ();
Holder. setValue ("xxx ");
// Use wildcard wildcards, but setValue cannot be called later. Otherwise, an error occurs during compilation.
Holder <? Extends Object> holder = new Holder <> ("xxx ");
From magic