I'm studying generics in Java, and was getting along comfortably until I reached the topic- Creating a generic method.
I know that in Java, generics are used when you want to implement something irrespective of the data type that the program(or method) operates upon. So, you could have a generic class as Class Gen<T>
and then in a non-generic class class GenDemo (which includes the main()
). Then, you can create Gen references
for different data types, such as Gen<Integer> iOB
and Gen <String> strOB
.
However, in the example given on creating a generic method, the book gives the following code:
//This is a simple generic method
class GenMethDemo
{
//determine if an object is in an array
static<T,V extends T> boolean isIn(T x, V[] y)
{
for (int i=0; i<y.length; i++)
if(x.equals(y[i]))
return true;
else
return false;
}
public static void main(String[] args)
{
//use isIn() on Integers
Integer nums[]={1,2,3,4,5};
if(isIn(2,nums))
System.out.println("2 is in nums");
if(!isIn(7,nums))
System.out.println("2 is in nums");
//use isIn() on Strings
String strs[]={"one", "two", "three", "four", "five"};
if(!(isIn("two", strs))
System.out.println("two is in strs");
}
}
I understand that this program is trying to determine if a given array consists of a specified object. But I can't wrap my head around this line:
static <T,V extends T> boolean isIn(T x, V[] y)
Thinking on the lines of what I've studied so far in generics, I know that there are two arguments of the Boolean function isIn()
. But what are the type-paramaters <T, V extends T>
doing before the return type of the function isIn()
?
I understand the use of the keyword extends here; it acts as a bound for the type-parameter V, ie: V must be of the same type as T or a subclass of T, but I can't get further.
There is a similar use of type-parameters under the topic: Creating Generic Constructors , as:
class GenCons
{
private double val;
<T extends Number> GenCons(T arg)
{
val=arg.doubleValue();
}
void showVal()
{
System.out.println("Val: "+ val);
}
}
As before, I'm stumped by the line: <T extends Number> GenCons(T arg)
. Why is <T extends Number>
used before the constructor is declared? It could also have been written like: GenCons(<T extends Number> arg)
?
Any help would be highly appreciated.
Notice how in your GenMethDemo
and GenCons
classes, the class itself doesn't have a generic type. It's not class GenMethDemo<T, V extends T>
-- instead, it's just class GenMethDemo
.
So if GenMethDemo
and GenCons
aren't generic classes, how are you able to use generics? It seems to be a contradiction.
Well, Java also lets you define generic methods . If I do static<T,V extends T> boolean isIn(T x, V[] y)
, it's as if I had actually done class GenMethDemo<T, V extends T>
except that the type variables T
and V
are scoped only to that particular method. This can be useful when you don't necessarily want the entire class to use generics; only a method or two where they're really needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.