Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <> mean for java generics?

Tags:

java

generics

I have a bit of code:

class MyClass<RCM>    private List<RCM> allPreExistingConfigsForCodes() {     if(this.allCodesForThisType.size() == 0)        return new ArrayList<RCM>(0); 

IntelliJ is telling me I should replace new ArrayList<RCM> with new ArrayList<> what would that mean?

like image 948
George Mauer Avatar asked Dec 28 '11 19:12

George Mauer


People also ask

How do you declare a generic variable in Java?

A Generic Version of the Box Class To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.

What does <> in Java mean?

<> is used to indicate generics in Java.

What is generics in Java with example?

The Java Generics allows us to create a single class, interface, and method that can be used with different types of data (objects). This helps us to reuse our code. Note: Generics does not work with primitive types ( int , float , char , etc).

What are Java Generics used for?

Java Generics helps the programmer to reuse the code for whatever type he/she wishes. For instance, a programmer writes a generic method for sorting an array of objects. Generics allow the programmer to use the same method for Integer arrays, Double arrays, and even String arrays.


2 Answers

From the Java Tutorials generics lesson:

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box<Integer> with the following statement:

Box<Integer> integerBox = new Box<>();

like image 148
Paul Bellora Avatar answered Sep 21 '22 09:09

Paul Bellora


Are you using Java 7? If so, it is trying to take advantage of the new "diamond notation."

http://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation

like image 21
Jeff Storey Avatar answered Sep 22 '22 09:09

Jeff Storey