• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unchecked cast/conversion warnings

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sometimes i am glad i am still in java2 land. i get warnings now and then if i recompile, but i just ignore them
 
Marcus Kelvin
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That error shows that yoiu ought not to create classes with the same names as types in the java.lang package.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic