-
Bug
-
Resolution: Fixed
-
P4
-
None
-
b96
-
generic
-
generic
-
Verified
When the following program is compiled with -XDfind=diamond:
// ----- 8< -------
public class X<T> {
T field1;
public X(T param){
field1 = param;
}
public static void main(String[] args) {
X.testFunction(new X<Object>("hello").getField()); //prints 2
X.testFunction(new X<>("hello").getField()); // prints 1
}
public static void testFunction(String param){
System.out.println(1);
}
public static void testFunction(Object param){
System.out.println(2);
}
public T getField(){
return field1;
}
}
javac issues a recommendation:
/home/srikanth/tmp/X.java:7: warning: Redundant type arguments in new expression (use diamond operator instead).
X.testFunction(new X<Object>("hello").getField()); //prints 2
^
explicit: X<Object>
inferred: X<String>
1 warning
The type argument <Object> is actually not redundant. Removing it and using the diamond
operator instead per javac recommendation would actually change the meaning of the program.
(instead of printing 2 followed by 1, it would actually print 1 followed by 1).
// ----- 8< -------
public class X<T> {
T field1;
public X(T param){
field1 = param;
}
public static void main(String[] args) {
X.testFunction(new X<Object>("hello").getField()); //prints 2
X.testFunction(new X<>("hello").getField()); // prints 1
}
public static void testFunction(String param){
System.out.println(1);
}
public static void testFunction(Object param){
System.out.println(2);
}
public T getField(){
return field1;
}
}
javac issues a recommendation:
/home/srikanth/tmp/X.java:7: warning: Redundant type arguments in new expression (use diamond operator instead).
X.testFunction(new X<Object>("hello").getField()); //prints 2
^
explicit: X<Object>
inferred: X<String>
1 warning
The type argument <Object> is actually not redundant. Removing it and using the diamond
operator instead per javac recommendation would actually change the meaning of the program.
(instead of printing 2 followed by 1, it would actually print 1 followed by 1).