aspose file tools
The moose likes Beginning Java and the fly likes Type mismatch with generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Type mismatch with generics" Watch "Type mismatch with generics" New topic
Author

Type mismatch with generics

Paul Berry
Greenhorn

Joined: Apr 07, 2011
Posts: 15
Me and generics have yet to make peace. For something that *seems* so simple, it has been battle after battle.

I'm trying to make a Comparator to help with a sorting task. Below is a hacked down version of the class which still gives the error.
I'm not understanding why it won't compile. The error message doesn't help, because to me it says "Error ...apple does not equal apple!"
I gave it <T extends String> after having problems with a simple <T>. I figured that it didn't have enough information so I added the constraint. ("extends String" means String or lower in inheritance right?) But still it will not compile if I try to use any String methods on what I believe to be a String. Other attempts just yielded "cannot find symbol" errors. String is in java.lang, so it must be able to find it right? Anyway, any help on this would be great.

Thanks,
Paul



TestComparator.java:7: error: incompatible types
T myVariable = o1.toString();
^
required: T
found: String
where T is a type-variable:
T extends String declared in class TestComparator
1 error


The Hanging Kludge
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Let's look at this code:

The toString() method returns a String. But T is some subclass of String. If you're going to assign an object to a variable of a subtype like that, you have to use a cast.

This is a basic rule of Java which has nothing at all to do with Generics. And Generics doesn't make that rule go away. Here's another example of the rule which you might understand more clearly:

String is a subclass of Object, you say, so why can't we do that assignment? Because the thing you assign to a String variable must be a String, or a subclass of String. Object won't do.

It's exactly the same with your Generics code, just obfuscated a bit.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Note that class String is final - it is not possible to subclass it. So declaring a type parameter "T extends String" doesn't really make sense - the only type T that can satisfy the restriction is class String itself. This means that you don't need the type parameter at all, you could just as well just use String instead of the type parameter T.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Berry
Greenhorn

Joined: Apr 07, 2011
Posts: 15
OK guys thank you. I'll have to remember that even though something is obvious to me, the compiler may not have enough information to determine; or guarantee.
 
I agree. Here's the link: jrebel
 
subject: Type mismatch with generics
 
Similar Threads
Generics
generics problem
Comparable and generics
generics
Generics <?>