• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic problem  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I want to use < Integer>

It shows this error:

C:NetBeansProjects\square\src\square\square.java:15: operator * cannot be applied to T,T

 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you using the class in your code? Please post the complete code.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per your code, you have defined the 'square' class generically such that it can take a type T and work on with it. There are no restrictions to T. So when compiling it wont allow you to do a multiplication operation - because what if you say new square<String>()? In that case, '*' can never work. So, what you must do is post some restrictions on the type 'T' - like 'T extends Integer'. By doing this, you are ensuring that the type T shall always be something that extends Integer and '*' can be safely applied on it.

I've did some changes to your code. It is because that T t = i * i; wont be possible as the compiler wont know what T is, and shall not allow you assigning an Integer to a T.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or if you do not want to restrict your generic class type T to an Integer, you can optionally restrict the constructor alone. Like this...

Here still you can create new square<String>(), but for that you'll have to define a new constructor. The above constructor shall be called only for Integer parameter constructor invocation.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Integer is final, there is only one class allowed for T - Integer itself. You can just as well remove the entire generic type T completely.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

http://download.oracle.com/javase/tutorial/java/generics/gentypes.html
In this code generic works well without any restriction or using extends or...

Then this code must work:


Pass integer, and It must work.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are not doing a multiplication. Vinoth already explained this in his post.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:

In this code generic works well without any restriction or using extends or...


Here, there is no code that wants a restriction.
In that code of the square class, we had a multiplication operator applied on the generic type. So it required some sort of restriction, as you can do multiplication on numbers only.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about that :
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that code were C++, it would work.
However, C++ template and Java generic is completely different.

In C++, when you type square<int>, the compiler makes the whole new class named square<int>
However, in Java, square<T> is just the only one square<T> class.

If you type public class square<T>, when you use object of type T in your square class,
you can use Object's methods only.

Likewise, if you type public class square<T extends Number>, only methods of Number are allowed.

For *, it is allowed for Integer, Float, Double, but not for Number or Object.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why this cod doesn't work?
Error is about incompatible type
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The result of i*i is int (may be converted to Integer), but you're trying to assing this value to a variable whose type is some subtype of Integer.
Like Integer x=new Object() is not allowed, this assignment is not allowed.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My IDE warns me about line 1: Integer is a final class, so saying "T extends Integer" does not make sense - nothing can extend Integer because Integer is final.

Because of this, the only possible type that T could stand for is Integer itself - and if T is always Integer, it isn't even necessary to make this class generic at all. You could just as well have written this:

Note, by the way, that the member variable i in line 2 is never referenced at all - the argument i in line 3 shadows it.

But the real problem is this: You get a compiler error in line 4 because it is not possible to assign the result of the calculation to i, which is of type T. Suppose that Integer was not final and that there would exist a subclass of Integer. The compiler would not know how to box the int, that's the result of the multiplication, into a variable of type T.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!