Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2004

Tip: Looking for answers? Try searching our database.

Strange compiler error with generics

Thread view: 
Oliver Plohmann - 29 Oct 2004 10:28 GMT
Hello!

I get some compiler error I don't understand when implementing a
parameterized static method:

Bounds mismatch: The generic method sort(List<T>) of type Collection
is not applicable for the arguments (List<Integer>) since the type
Integer is not a valid substitute for the bounded parameter <T extends
Comparable<? super T>> Foo.java

I have removed all the code that has nothing to do with the problem.
So the code below does not necessarily do much. Methods foo1() and
foo2() compile fine, but method foo3() creates the compiler error. I'd
be glad if someone could tell me what the problem is here.

Cheers, Oliver Plohmann

public class Foo
{
    public static void foo1()
    {
        List<Integer> list = new ArrayList<Integer>();
        Collections.sort(list);    // compiles without error
    }

    public static <T> void foo2()
    {
        List<Integer> list = new ArrayList<Integer>();
        Collections.sort(list);    // compiles without error
    }

    public static <Integer> void foo3()
    {
        List<Integer> list = new ArrayList<Integer>();
        Collections.sort(list);    // compiler error
    }
}
Larry Barowski - 30 Oct 2004 07:03 GMT
> Hello!
>
[quoted text clipped - 8 lines]
> }
> }

You have used "Integer" as a generic parameter name.
That is a bad idea. You can continue to use "Integer"
in this way, and bound it correctly:

public static <Integer extends Comparable<? super Integer>> void foo3()
{
  List<Integer> list = new ArrayList<Integer>();
  Collections.sort(list);
}

but that is probably not what you want to do. You
could also qualify the uses of "Integer" that you
want to mean "java.lang.Integer".

public static <Integer> void foo3()
{
 List<java.lang.Integer> list = new ArrayList<java.lang.Integer>();
 Collections.sort(list);
}

But  that is also confusing. I wouldn't use "Integer"
as an ordinary variable name either, even though
it might do no harm.
Oliver Plohmann - 30 Oct 2004 20:00 GMT
> > Hello!
> >
[quoted text clipped - 32 lines]
> as an ordinary variable name either, even though
> it might do no harm.

I fear I stripped off too much code in my snippet. Now, the problem is
not visible any more. Here is a less reduced version of foo3:

public static <K, Integer> List<K> foo3(Map<K, Integer> map)
{
   List<Integer> values = new ArrayList<Integer>();
   for(Integer i : map.values())
       values.add(i);
   Collections.sort(values);    //    compiler error happens here
}

I found the problem now: it is caused by "public static <K, Integer>".
The compiler error disappears when it is changed to "public static
<K>". Just wanted to pass this on.

Regards, Oliver
Larry Barowski - 31 Oct 2004 05:42 GMT
> I fear I stripped off too much code in my snippet. Now, the problem is
> not visible any more. Here is a less reduced version of foo3:
[quoted text clipped - 10 lines]
> The compiler error disappears when it is changed to "public static
> <K>". Just wanted to pass this on.

This is the same problem I explained - you have used "Integer"
as a generic type parameter name.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.