Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic Class: Calculating with generic Typs  RSS feed

 
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
Posts: 21048
85
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!