Issue with Subtyping and Autoboxing
Hi,
I have a problem with subtyping and autoboxing. Can anybody please help me to know the reason for this.
publicclass GenericSubType<Textends Integer>{
T iVal;
public GenericSubType(T t){
iVal = t;
}
publicstaticvoid main(String args[]){
GenericSubType<Integer> gst =new GenericSubType<Integer>(30);
System.out.println("gst.iVal = " + gst.iVal);
}
}
The above code is giving a compile time error saying that:
GenericSubType.java:8: cannot find symbol
symbol : method valueOf(int)
location : bound of type variable T
GenericSubType<Integer>gst = new GenericSubType<Integer>(30);
Fatal Error: Unable to find method valueOf
If we change <Textends Integer>
to <Textends Number>
in the class declaration the compile time error will be resolved and the program works as expected.
class GenericSubType<Textends Number>{
// Body of the class...
}
Can anybody tell what is the problem with the original code and how it is affecting?
Expecting a justifiable reason.
Regards,
Thomas.
# 3
> I don't think so. Try replacing Integer with
> String and pass the appropriate String
> arguments instead of Integer arguments. It
> will work!!! What I guess as a problem is something
> related to autoboxing only, but I couldn't figure out
> what it could be in autoboxing.
I have Eclipse installed (which does not make use of Sun's compiler) and I have no problem with the code you posted in the first place.
So I can't be of any help, I'm afraid.
# 5
I'm not sure, if this is javac problem, but actually, what is the purpose of parameterizing a class with an upper bound Integer? That's a purely academic problem and I don't think javac should care about it.
Actually, even eclipse is giving a warning on "The type parameter T should not be bounded by the final type Integer. Final types cannot be further
extended".
To me it seems that using autoboxing in the same breath with instantiating the parameterized class causes the problem.
# 6
> I'm not sure, if this is javac problem, but actually,
> what is the purpose of parameterizing a class with an
> upper bound Integer? That's a purely academic problem
Maybe autogenerated code or such...
anyway I don't think it's an error, just like is not an error to use an unecessary cast.
but the message and location given by javac "cannot find symbol valueOf" is an error IMHO
[]
# 10
> It's still useless and over-generified code. If I
> were a Java compiler, I would throw it at you, too ;)
lucky me., you are not my compiler ;--)
you propably would not compile something likeint val = something();
BigDecimal big = new BigDecimal((double)val);
since there is no need for explicit casting... but I need that to make my program compatible with 1.4 :--/
# 11
Why shouldn't I compile unnecessary statements? You only would see a warning on unnecessary cast here ;)
The OP instead is introducing a generic parameter having a final class as bound, which pointless as for any instance the generic argument could be nothing but that very class. Why would anyone need it?
I'm quite sure it's a javac bug (or maybe optimization issue in javac) in the combined play of autoboxing and generics. I'd say it tries to autobox the int into a T without taking into account that T always is Integer. But T obviously has no valueOf(int) method.