Reputation: 6052
I am currently new to working with Java. So far I have been able to easily use the basic such as Classes
, Functions
, Arrays
etc from my knowledge of JavaScript and PHP.
However, what I have never seen before is this: <>
. Inside of that is a variable of some type. Because I don't know what it is called, I can't find any answers.
I've seen them like this:
List<String> myList = new ArrayList<String>();
But also like:
public static <T> boolean contains( final T[] array, final T v ) {
for ( final T e : array )
if ( e == v || v != null && v.equals( e ) )
return true;
return false;
}
What does the <String>
mean?
In the function, I was also wondering what is specifically special about the T
?
Upvotes: 1
Views: 199
Reputation: 83
What you have observed is partially correct! In Java, inside <> should be 'Object' type or any subclass type of 'Object'.
When you declare myList as below:
List<String> myList = new ArrayList<String>()
, it ensures the declared and initialized myList has elements of only String type. This is called type declarations.
public static <T> boolean contains( final T[] array, final T v ) { }
, is a generic method declaration which implies that the passed two parameters should be of same object type, 1st parameter an array, 2nd parameter an object. And the method return type also should be same type.
Refer Java docs for more examples on Generics. http://docs.oracle.com/javase/tutorial/extra/generics/methods.html
Upvotes: 0
Reputation: 13556
That defines the Type
of the objects a collection can hold
so when you write List<String> myList = new ArrayList<String>();
, it means
Create an arrayList that can hold the String objects.
SomeParameterizedClass{ T someValue; }
means that you can pass your type to the class e.g. SomeParameterizedClass<String>
so that someValue
becomes of type String
When you write List<T> list = new ArrayList<T>();
, here T can be any type.
With java 7 you can simply say List<String> list = new ArrayList<>();
. It has the same meaning.
In java 7 <>
is called as diamond operator
Upvotes: 2
Reputation: 17422
You will find < > in the following cases, the T is a "parameter type" in plain English that means you could "substitute" that T by any other non-primitive type (like String
or Integer
):
When you are declaring a parameterized Type:
class MyParameterizedClass<T> {
public T myValue;
}
When you are declaring variables of your parameterized type:
MyParameterizedClass<String> myStringParam;
MyParameterizedClass<Integer> myIntegerParam;
When you are using constructors of parameterized types:
MyParameterizedClass<String> myStringParam = new MyParameterizedClass<String>();
MyParameterizedClass<Integer> myIntegerParam = new MyParameterizedClass<Integer>();
myStringParam.myValue = "Hello world";
myIntegerParam.myValue = 5;
When you declare a generic method:
public <T> T updateMyValue(T myValue) {
this.myValue = myValue
}
Upvotes: 0
Reputation: 1976
This is for a Generic type
What it allows you to do is to pass through a type, and make it useful for multiple object types
So List is a generic collection, and it allows you to make a list of any object. Making List<String>
will make the object be a list of Strings. Or you could use List<MyClassType>
and it would make a list of objects of your class
Upvotes: 5