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

5
  • 5
    is this question a "generic joke"? google search please. Commented Feb 25, 2013 at 9:37
  • this is to specify the object type. Commented Feb 25, 2013 at 9:38
  • 1
    Hit the Java tutorial first: docs.oracle.com/javase/tutorial/java/generics (That chapter and the rest of it.) Commented Feb 25, 2013 at 9:40
  • Learn about generics.. Commented Feb 25, 2013 at 9:40
  • 1
    Seems like a GIVEAWAY of REPUTATION for free ;) everybody cashing in :D Commented Feb 25, 2013 at 9:43

10 Answers 10

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:

Sign up to request clarification or add additional context in comments.

1 Comment

can the downvoter leave a coment ??
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/

Comments

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.

Comments

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.

Comments

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.

Comments

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

Comments

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

Comments

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.

2 Comments

It is used for parametrized Arraylist.. Not true, Generics is not only used for ArrayList, in fact, the whole Collections API is generi-fied.
Yes.. In current context.
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""

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.