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.
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.
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.
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.