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>
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>
<> 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
It is part of Java Generics introduced in version 1.5.
Following link might be useful: http://docs.oracle.com/javase/tutorial/java/generics/
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.
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.
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
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.
It is used for parametrized Arraylist.. Not true, Generics is not only used for ArrayList, in fact, the whole Collections API is generi-fied.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.