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.

[1874 byte] By [Thomas.Mathewa] at [2007-11-27 6:30:45]
# 1
That's probably because java.lang.Integer is a final class (which cannot be extended, of course).
prometheuzza at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 2

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.

Thomas.Mathewa at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 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.

prometheuzza at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 4
same here, with exlipse I only got a warning that Integer is final and cannot be further extended. Compiling with JDK I got the above error, seems to be a problem with javac
S_i_m_ua at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 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.

stefan.schulza at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 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

[]

S_i_m_ua at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 7
Unless, of course, javac is right (for some reason that's not obvious to any of us) and Eclipse is wrong...
dannyyatesa at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 8

It doen't seems to be of any obivious reason for javac to stop passing this code. Experts please comment. Moreover the Eclipse compiler is giving out a fair and adequate warning and passing the code which is highly expected rather than saying cannot find valueOf() or someother irrational messages.

Message was edited by:

Thomas.Mathew

Thomas.Mathewa at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 9
It's still useless and over-generified code. If I were a Java compiler, I would throw it at you, too ;)
stefan.schulza at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 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 :--/

S_i_m_ua at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...
# 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.

stefan.schulza at 2007-7-12 17:55:26 > top of Java-index,Core,Core APIs...