Question on Generic Method/Constructor & inference
Can anybody please give a scenario to the following question?
When to explicitly specify the type argument for a generic method or constructor?
Consider the generic methodshow() in classTest:class Test{
<U>void show(U u){
System.out.println("u = " + u);
}
}
This method can be called as either of the following cases:-
Case 1:
Test t =new Test();
t.show("Test String");
or
Case 2:
Test t =new Test();
t.<String>show("Test String");
In the first case, the type argument<U> is implicitly discerned to be asString through inference.
In the second case, type argument<U> is explicitly stated to be as<String>.
Other than better code readability and emphasizing the method argument to be of the specified type, can anybody tell me a scenario where the explicit stating of type argument to a method has further significance? I mean a scenario where specifying the type argument becomes mandatory.
Message was edited by:
Thomas.Mathew
# 4
But how the explicit type parameter specification becomes mandatory during a null argument pass? I tried it with a small sample program. Even though we specify the type parameter during a null argument there is no use in explicitly specifying the type parameter which makes it mandatory. Consider the below code:
I can feel as another case where explicity type parameter specifying is mandatory is whenever the arguments of the method are not of generic type. Eg:
class SomeClass {
<N extends Number> void show(String str) {
N n = (N)new Float(3.14f);
System.out.println(str + n);
System.out.println("Run Time Class of N is " + n.getClass().getName());
}
}
Call this method by:
...
SomeClass sc = new SomeClass();
System.out.println("Implicit Type specified call to show()");
sc.show("Hello PIE - ");
System.out.println(Explicit Type specified call to show());
sc.<Integer>show("Hello PIE - ");
...
With javac amazingly!!!
The output will be as follows:
Implicit Type specified call to show()
Hello PIE - 3.14
java.lang.Float
Explicit Type specified call to show()
Hello PIE - 3.14
java.lang.Float
The second call to show is not honoring the <Integer> type passed. Is it correct or not? How about other compiler if you say that javac is wrong?
null
# 5
Seems like your last question is about polymorphism mechanism, not generics, the erasure of your show method will be :
void show(String str) {
Number n = (Number) new Float(3.14f);
System.out.println(str + n);
System.out.println("Run Time Class of N is " + n.getClass().getName());
}
So why are you expecting a different output ?
n is a Float, so even if you cast it to a superclass, getClass().getName() will still return 'Float'.
Calling the toString method will execute the toString method of the Float class for the same reason.
((Object) new Float(3.14f)).toString() // returns '3.14'
((Number) new Float(3.14f)).toString() // returns '3.14'
Casting to Integer would not compile :
((Integer) new Float(3.14f)).toString() // compile-time error
Intermediate cast to a superclass, Number or Object, would compile but throw a ClassCastException at runtime:
((Integer) (Number) new Float(3.14f)).toString() // runtime error
If you're expecting the float to be rounded, that only works with primitive types.
System.out.println((int) 3.14f); // => prints 3