aspose file tools*
The moose likes Beginning Java and the fly likes Unchecked cast/conversion warnings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unchecked cast/conversion warnings" Watch "Unchecked cast/conversion warnings" New topic
Author

Unchecked cast/conversion warnings

Marcus Kelvin
Ranch Hand

Joined: Jan 04, 2012
Posts: 44
I have a comparator that I can't prevent from generating "unchecked cast" warnings. This is the least angry version I've been able to come up with:



But I still get:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

You don't need a cast there, you just need to declare the type of your reportComparator. The error message even tells you how:


(And by the way, class names in Java should start with a capital letter. It's variable names which should start with a lower-case letter.)
Marcus Kelvin
Ranch Hand

Joined: Jan 04, 2012
Posts: 44
Paul Clapham wrote:You don't need a cast there, you just need to declare the type of your reportComparator. The error message even tells you how:


Excuse my frustration, but declare what where? I have tried replacing either or both T's here with String:


This only leads to more serious looking warnings. So where am I suppose to refer to a reportComparator<String>?

If anyone is so bold as to provide an actual example of this corrected (it seems like something commonplace), it would be much appreciated; otherwise it is just too ambiguous -- I do not see the issue and am chucking darts in the dark.


(And by the way, class names in Java should start with a capital letter. It's variable names which should start with a lower-case letter.)


Thanks for the tip.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

At line 10 (the one which all the error messages are pointing at):

-- as the error messages said, with little ^ things pointing at the place I changed.
Marcus Kelvin
Ranch Hand

Joined: Jan 04, 2012
Posts: 44
Phew! Okay, your patience is appreciated.

That got rid of the warnings, which led me to try what I originally wanted to do -- make the comparator exclusively for strings:


This would save some further casting or re-assignment. Previously I got groans to the effect that ReportComparator must implement "compare (Object, Object)" which I took to mean that comparators must be more or less totally generic...but maybe not? Problem is now I get:



Why is that? Aren't lastIndexOf() and substring() String methods? Can I do this or should I just go back to using casts/reassignments inside a (Object, Object) function?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

Marcus Kelvin wrote:Aren't lastIndexOf() and substring() String methods?


Well, no, they aren't. Normally they would be, but if you read the error message

you'll see that in that code, String is not the class java.lang.String, it's an oddly-named type variable.

Can I step back a few hours and ask why you want your ReportComparator class to be generic? Why don't you just have

in which ReportComparator is just an ordinary, non-generic class?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

sometimes i am glad i am still in java2 land. i get warnings now and then if i recompile, but i just ignore them


SCJP
Visit my download page
Marcus Kelvin
Ranch Hand

Joined: Jan 04, 2012
Posts: 44
Paul Clapham wrote:
Marcus Kelvin wrote:Aren't lastIndexOf() and substring() String methods?


Well, no, they aren't. Normally they would be, but if you read the error message

you'll see that in that code, String is not the class java.lang.String, it's an oddly-named type variable.


Okay. This I noticed. So what is "type-variable" in relation to the class java.lang.String?

Can I step back a few hours and ask why you want your ReportComparator class to be generic? Why don't you just have

in which ReportComparator is just an ordinary, non-generic class?


This is day 2. I'm hoping the language makes more sense soon...with your help it may, lol, because now I have:



O_O No warnings, no errors, works with simple tests, looks rational. So this hinges on typing "implements Comparator"<correctly> ?

Randall Twede wrote:
sometimes i am glad i am still in java2 land. i get warnings now and then if i recompile, but i just ignore them


What's java2?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Java 2 is the collected name for Java 1.2 to 1.4. After that we got Java 5.0 and Java 6, and we're now at Java 7.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
That error shows that yoiu ought not to create classes with the same names as types in the java.lang package.
 
 
subject: Unchecked cast/conversion warnings
 
Similar Threads
generic class: what am i doing wrong?
Doubt in OCP Practice Exams question
Warning error implementing Comparator
Warning error ..?
Generics Warnings