aspose file tools*
The moose likes Beginning Java and the fly likes Generic Class: Calculating with generic Typs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic Class: Calculating with generic Typs" Watch "Generic Class: Calculating with generic Typs" New topic
Author

Generic Class: Calculating with generic Typs

Wolfgang Troescher
Greenhorn

Joined: Feb 12, 2010
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wolfgang Troescher
Greenhorn

Joined: Feb 12, 2010
Posts: 14
OK! Then my assumption was right, but was not sure!

THANK YOU!!!

Wolfgang
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Class: Calculating with generic Typs
 
Similar Threads
generic not recognised
Generics Doubt
Wildcards in Class declaration
Generic Declarations
Doubt in generic method