• 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

Cast issue (when using generics)

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Given the code below, I get the following compiler error: Type mismatch: cannot convert from Object to String
What I don't understand, is why the compiler is unable to resolve T to String in this case (by simply removing <X> (which isn't used), or eg declaring newShape as Shape<Integer> all works fine)

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robert,

Welcome to JavaRanch!

That's an interesting observation, and I don't have a ready explanation for it. I suspect the answer is to be found in this section of the language spec, but only with a lot of heavy lifting:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#341287

 
Sheriff
Posts: 22781
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
Apparently when you drop generics in the declaration, it drops generics completely. Not just the class' generic types but also the method generic types. If you change the declaration in the easiest thing (Shape<?> newShape = ...) it works again.
reply
    Bookmark Topic Watch Topic
  • New Topic