Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics question" Watch "Generics question" New topic
Author

Generics question

Kris Ram
Greenhorn

Joined: Jul 16, 2012
Posts: 10
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

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Aug 07, 2007
Posts: 2339
    
  50
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

Joined: Aug 16, 2005
Posts: 14279
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8060
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Kris Ram
Greenhorn

Joined: Jul 16, 2012
Posts: 10
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

Joined: Mar 17, 2011
Posts: 8060
    
  22

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
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8060
    
  22

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

Joined: Aug 16, 2005
Posts: 14279
    
  21

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

Joined: Mar 17, 2011
Posts: 8060
    
  22

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics question