Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Class: Calculating with generic Typs

 
Wolfgang Troescher
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Probably a stupid question, but why can´t I calculate with generic Variables? :


The error "operator + can´t be applied to T,T" appears also if I change <T> to <T extends Number>. Is it generally forbidden to calculate with generic types?

Thank you!
Wolfgang
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With an unbound T, you can use anything for T. That includes classes like File and even Object. What would + mean for two File objects, or for two Object methods?

If you bound T to Number then it would be possible for Integer, Short, etc through auto(un)boxing. But what about BigDecimal or BigInteger? They don't support auto(un)boxing. And what about any custom Number subclass?

So no, this is not going to be possible.
 
Wolfgang Troescher
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK! Then my assumption was right, but was not sure!

THANK YOU!!!

Wolfgang
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic