This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 19651
    
  18

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
Doubt in generic method
Generics Doubt
generic not recognised
Wildcards in Class declaration
Generic Declarations