Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics question

 
Kris Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

I am trying to create a small generic calculator. It should be able to add any type of number.

The code is shown below and I am getting an error on the return line. My IDE shows the following error 'operator + is undefined for auguements type'.

How am I expected to code a generic sum method?

p.s: Links to any documentation will be appreciated.



Cheers
 
Stephan van Hulst
Bartender
Pie
Posts: 5813
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't, because the + operator isn't defined for generic numbers. You can accept generic numbers as parameters, but the return value will never be generic, because you can't create new instances. Generics generally aren't useful if you can't use them as return types, so there's no point in doing this.
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a way around the problem (albeit not a very nice one) which is to pass a third parameter into the method which is the Class of the generic objects. You can then use the Class object to create a new instance of the generic object. This is only useful if you know all the objects will have a common base type such as Number so you know what methods you can call on the objects. You can do this by declaring your class as being a generic type of T which is a sub class of Number.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you were programming in C++ with templates, this would be possible. But Java generics work differently than C++ templates, even though they look similar. Maybe you've programmed in C++ before and this confuses you.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris Ram wrote:How am I expected to code a generic sum method?

Actually, there is a way to do it; but, like Tony's says it's not nice, and will probably be VERY slow.

You could create a Result class that extends Number and wraps a BigDecimal with your operands. You do the calculation with the BigDecimal and then return the result based on the operand type (Number classes - and BigDecimal is a Number - have methods to return all major numeric types).

Of course, then there's precision and rounding modes to consider; not to mention the fact that someone might try to hand you a non-standard Number class....

You know what? Stephan's right, it's probably more trouble than it's worth.

Winston
 
Kris Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys,

although I have to say I am a bit confused. One of the reason generics was introduced was to solve situations like this one, i.e create a generic method which could be reused.

How would you guys implement a generic sum() method for a Calculator.


Cheers
Kris
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris Ram wrote:One of the reason generics was introduced was to solve situations like this one, i.e create a generic method which could be reused.

No it wasn't. It was introduced primarily to solve casting issues. And I think even it's firmest advocates (like me) would say that it's NOT a 100% solution.

How would you guys implement a generic sum() method for a Calculator.

Why do you think you need to? I've already given you my idea, and I can't honestly see that a "type-based" calculator is actually all that useful.

Winston
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
How would you guys implement a generic sum() method for a Calculator.

Why do you think you need to? I've already given you my idea, and I can't honestly see that a "type-based" calculator is actually all that useful.


Viability of the question aside, my response is ... How do you add two cats? How do you add two bottles of wine? How do you add two pine trees? Or basically, how do you add two Widgets, that only I know about? The answer is "you can't". Certainly not without reading my mind and understanding what it means to add two of my widgets.

Generics or not, in order to do something by computer, you need to understand how to do it on paper. In this case, if you can't add two generic objects on paper, how do you expect the computer to do it?


To answer the question, you need to limit the scope of the generics. The generics has to be for object where it is possible to do it on paper, and then you do it by computer. For example, what if you require all your generic objects to implement some sort of Summable interface? Heck, or you can limit it to objects that implement the Number interface. For the second case, you can convert all your numbers to doubles to sum it up (or something like that).

Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Viability of the question aside, my response is ... How do you add two cats? How do you add two bottles of wine? How do you add two pine trees?

Hmmm. Not quite the same thing, IMO. Java has a Number interface, so one might ask: why can't you add two Numbers? Seems a reasonable question to me.

Indeed, one might ask why Integer (a Number subclass) doesn't have a
public final Integer add(Integer i)
method? BigInteger does; and so does BigDecimal.
Or, indeed, why Number isn't defined as:
Number<T extends Number<T>>

@Kris: The fact is that it isn't, and you just have to live with it; and the reasons probably have much more to do with how you would implement an arithmetic system involving possibly unknown Number types (Number is an interface, remember), rather than any actual issues with generics.

And I'd also think about about my previous question: what does a
public <T extends Number> T add(T v1, T v2)
method actually do for you? Where would you use it, apart from your "type-based calculator"?
It's also worth noting that it's not the same thing as the instance equivalent above.

Winston
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Actually, there is a way to do it; but, like Tony's says it's not nice, and will probably be VERY slow.

There might be some very cumbersome way to do it, but it's simply impossible in Java to use the + operator (or any other operator) on some generic type T.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:There might be some very cumbersome way to do it, but it's simply impossible in Java to use the + operator (or any other operator) on some generic type T.

True, but I think the question was geared more to writing a generic sum() method, rather than whether you actually implement it with a '+'.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic