• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Generic problem

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about that :
 
Ranch Hand
Posts: 107
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You didn't tell me he was so big. Unlike this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic