-1

I am new to java. I would like to know what is <> used for in java.

This is an example where I get confused: List<File> sourceFileList = new ArrayList<File>

CosminO
  • 5,018
  • 6
  • 28
  • 50
su_darell
  • 25
  • 1
  • 1
  • 2

10 Answers10

4

<> is a place holder which hold's generic type. you embed the Type Parameter in the angle brackets.

List<File> sourceFileList = new ArrayList<File>

The above piece of code describes that your List can only have instance of type File. It provides compile time type safety. you can only add File/sub type of File Objects into the list.

       sourceFileList.add(new File("test.txt"));
       sourceFileList.add("abc");// compiler error as your
                                  list only accepts File instances

Links:

PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

It is part of Java Generics introduced in version 1.5.

Following link might be useful: http://docs.oracle.com/javase/tutorial/java/generics/

tom
  • 2,735
  • 21
  • 35
1

<> are generally used for Generic data types in java.

So here List means that you are having a list of files.

So if you write List<Person> it will become list of persons. Thus you can replace the text within <> with any class' object.

Sanober Malik
  • 2,765
  • 23
  • 30
0

It is to tell the compiler what kind of data is going into the object. For example, List<File> tells java that you want to create a List that will be filled with File type data. For another example: Array<Integer> would tell java you want an Array that you will be filling with Integer data.

L0j1k
  • 12,255
  • 7
  • 53
  • 65
0

Java Generics (not to be confused with C++ templating). It allows you to define types when defining classes that are Generi-fied.

In your case, the List<File> states that you have a List containing types of File.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

You should take a look at the java generics tutorial:

http://docs.oracle.com/javase/tutorial/java/generics/

And if you are really into it, check out the java language specification, specifically the part about generics:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.2

vainolo
  • 6,907
  • 4
  • 24
  • 47
0

In your example the <> is being used to instantiate an ArrayList of File objects. The <> merely specifies the type of objects your ArrayList holds

TangoKilo
  • 1,785
  • 3
  • 25
  • 43
0

Generics...

We stored String objects in the array list and to retrieve an object, we had to type cast it. Only programmer knows which objects he has stored in the ArrayList, so he is responsible to type cast it in the required type. What if, he by mistake casts it into wrong type?

Java Code:

System.out.println((Integer)myArrayList.get(3));

Error: Exception in thread "main" java.lang.ClassCastException: java.lang.String

To avoid such conditions, generics come into play. We can specify the type for a List so that list can only hold or store objects of that very type. Objects of some other types wont be stored into the list and no type casting is required then.

Java Code:

ArrayList<String> myArrayList = new ArrayList<String>();

myArrayList can not only store ‘String objects. Type safety is ensured and now programmer has better control over the array list.

The type is specified in angle brackets when we are declaring an instance of a class or interface. Without generics the type parameters are omitted, but one must explicitly cast whenever an element is extracted from the list.

Anurag Shukla
  • 195
  • 2
  • 6
0

If you want to translate your code into English to read it, you could say "of type" when you see <>.

eg for

List<File> sourceFileList 

say "List of type File, "sourceFileList""

user1725145
  • 3,993
  • 2
  • 37
  • 58
0

Datatype of your List. Example:

ArrayList<String> yourArrayList = new ArrayList<String>;

It declares your ArrayList as a String. Instead of using yourArrayList.get(index).toString(); you may use yourArrayList.get(index); and will return a String(In this example) and will only accept String datatype.

Michael 'Maik' Ardan
  • 4,213
  • 9
  • 37
  • 60