-2

Im writing an exercise program that completed in the past using generic glasses but now I've encountered a very strange error that I haven't seen yet.

Test.java:26: error: type argument String is not within bounds of type-variable
T
                Bag<String> b = new Bag<String>();
                ^
where T is a type-variable:
T extends Comparable<T> declared in class Bag

From what I make it's saying that class String does not implement the Comparable class. I've messed around with it for a while and I guess I'm making a small error somewhere but I can't see to see it. Any help would be appriciated. The code is:

class Bag<T extends Comparable<T>>
{
    private T [] b = (T [])(new Comparable[100]); ;
    int compareTo(T t)
    {
        return 0;
    }
}
class Test
{
    public static void main(String [] args)
    {
        Bag<String> bag = new Bag<>();
    }
}  
Aaron
  • 1
  • 2
  • Looks fine here: http://ideone.com/oi8b2A. Please construct a [minimal test-case](http://stackoverflow.com/help/mcve) that demonstrates the issue. – Oliver Charlesworth Jul 25 '15 at 22:03
  • [String](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) does implement `Comparable` – dramzy Jul 25 '15 at 22:05
  • The error is strange, String does indeed implement Comparable, I have no idea why it's erturning that error – Aaron Jul 25 '15 at 22:08
  • It's been edited to include the minimal test-case, apologies – Aaron Jul 25 '15 at 22:13
  • 2
    Your minimal test-case isn't a test-case; it also compiles fine: http://ideone.com/YZHozJ. Either you have a typo, a broken compiler, or something weird like a custom implementation of `String` on your classpath. – Oliver Charlesworth Jul 25 '15 at 22:16
  • Here's a more minimal case: `class Bag> { Bag bag = new Bag<>(); }` and it compiles without error. – Marko Topolnik Jul 25 '15 at 22:18
  • Hmm, I guess even after reading the description I don't know what a minimal test-case is. What I've shown is the bare minimum amout of code needed to produce the problem. The only thing poreventing me from thinking the issue isn't code related is it's happened on a few machines, mine and several colleagues. This is the terminal when attempting to compile that code exactly http://s18.postimg.org/ytnghdbuh/Hello.png – Aaron Jul 25 '15 at 22:27
  • Your `private T [] b` and `int compareTo(T t)` are surely dispensable. They have nothing to do with the problem with type inference. – Marko Topolnik Jul 25 '15 at 22:31
  • It would seem it has to be a broken compiler or something. It's really strange that the same issue has been happening on several machines though. I haven't tampered with the classpath at all. Even with the minimal test-case from Marko (ty for that) I get the same error. Very odd, thanks anyway guys – Aaron Jul 25 '15 at 22:36
  • http://stackoverflow.com/a/22650343/2869791 – Sarath Chandra Jul 25 '15 at 22:55
  • 1
    Maybe you have an `import` of some other `Comparable` in your file. Please, replace your `main` with `System.out.println(Comparable.class); System.out.println(String.class);`, run it and show the results. – Roman Jul 25 '15 at 22:57
  • I've done that there, this being the result: http://s8.postimg.org/5y1poccgl/Ayy.png – Aaron Jul 26 '15 at 00:45

1 Answers1

3

Your Comparable is not a java.lang.Comparable, but your custom interface, located in the same (default) package as Test. So the compiler is correct: java.lang.String does not implement that interface of yours.

Roman
  • 6,486
  • 2
  • 23
  • 41