• 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

Trying to understand Generics inferring process

 
Greenhorn
Posts: 29
Google Web Toolkit jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo all the beautiful people in this forum :-)

I am desperatelly trying to understand how generics do work and this problem puzzles me!

Why does this code run and give an exception back?:




I would expect that as with

F extends Comparable<F>

the compiler would protest:


He infers Number as global common Ancestor und this doesn't implement Comparable. OK. But why in the first case infers the compiler differently??

Thank you for your help in advance!
Greetings
Fabio

 
Fabio Salvi
Greenhorn
Posts: 29
Google Web Toolkit jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo all,

my hypothesis:

Given that Comparable Raw Type so definiert is



Then in my example long->Long->Object and in compareTo tries Java the Float to Long to convert...

Could it so be?

Greetings
Fabio
 
Saloon Keeper
Posts: 15450
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because Long and Float are not mutually comparable.

You're expecting two instances of Comparable<F>, but Long is Comparable<Long> and Float is Comparable<Float>.
 
Greenhorn
Posts: 5
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Fabio,

problem here is, that you are using raw type: But you should really use: This would produce compile time error.

What happens in your case is what you actually mentioned - Comparable takes any Object and then at runtime it fails to cast that Object into your desired one.

In your example concrete type is infered from the first parameter - Float. And Float class has its compareTo method defined as follows: which obviously fails cast Long to Float.

Hope this helps and is correct
Best regards, Robert.
 
Fabio Salvi
Greenhorn
Posts: 29
Google Web Toolkit jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks you both!

:-)

Ciao
Fabio
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic