What Does ≪T≫ (Angle Brackets) Mean in Java

What do angle brackets mean as a part of the return type? <T> T

public <T> T queryForObject(String sql, Class<T> requiredType, Map<String, ?> args) throws DataAccessException {
  1. <T> ---> is not a return type you are declaring generic type at method level, so that you can use this type T inside your method.
  2. T ----> is a return type of your method

What does a tilde in angle brackets mean when creating a Java generic class?

It is just a shorthand for "same as in declaration".

Some IDEs, e.g. IntelliJ use this too.

The files on disk do not have this notation, which is only a compaction in the IDE GUI.

In Java, what does it mean when a type is followed by angle brackets (as in List<Foo>)?

They're known as generics in java, and templates in C++.

http://java.sun.com/developer/technicalArticles/J2SE/generics/

Generics angle brackets before static function call

Those are called Generic Methods.

Before Java 7 you had to specify the type of the Generic reference:

Util.<Integer, String>compare(p1, p2);

Now the compiler infers the type from the context.

Square and angle brackets in Java

Can anyone tell me why in Java Array is using square brackets?

Because JLS defines such a syntax, for creating arrays:

An array type is written as the name of an element type followed by some number of empty pairs of square brackets []. The number of bracket pairs indicates the depth of array nesting.



while for ArrayList they use angle brackets?

Because, ArrayList<T> is a Java class, which has a constructor, and braces a.k.a round brackets are used to invoke a constructor and create an object of a particular class.

P. S. These two are completely different concepts.

Why are the angle brackets before the return type omitted sometimes from the definition of a generic method

E is a type variable -- it stands in for some other type, like String or Integer. So just as you can't understand dst.add(pop()) without knowing where and how dst was defined, you can't understand a method declaration like popAll(Collection<E> dst) without knowing where and how the type variable E is defined. In the case of popAll, the type variable E is defined at the class level: Stack<E>: it's the type of the elements in the stack. You'll often even see it javadoc'd:

/**A Stack of elements
*
*@param E The type of elements in the stack */
public class Stack<E>{
public void popAll(Collection<E> dst){ ... }
}

On the other hand, when you see a method declaration like public <E> void ..., the type variable E is being declared (not referenced from some enclosing scope like the enclosing class). In fact most times when you see a method with its own type variable, it's a static method, so there is no enclosing instance of the class to establish the value of E.

In both cases, what is the E type variable doing? It's telling us how two different types have to relate to each other. In popAll, it's telling us that the element type of the collection you want to put the popped elements into has to match the element type of the stack you're popping them from.

Similarly, take the example from page 136:

public class ListUtils{
public static <E> E reduce(List<E> list, Function<E> f, E initVal);
}

Here, the E type variable tells us that the element type of list has to match the argument type of f and the type of initVal. The surrounding class does not define E for us, it's only meaningful in the scope of the reduce method declaration.

What do < and > mean such as implements Comparable<BigInteger>?

Read the Java Generics Tutorial. The thing between the angle brackets is a type parameter - Comparable is a generic class, and in this case the angle brackets mean that the class is comparable to other BigIntegers.

For a little more clarification in this case, have a look at the Javadocs for Comparable in 1.5. Note that it is declared as Comparable<T>, and that the compareTo method takes an argument of type T. The T is a type parameter that is "filled in" when the interface is used. Thus in this case, declaring you implement Comparable<BigInteger> implies that you must have a compareTo(BigInteger o) method. Another class might implement Comparable<String> meaning that it would have to implement a compareTo(String o) method.

Hopefully you can see the benefit from the above snippet. In 1.4, the signature of compareTo could only ever take an Object since all kinds of classes implemented Comparable and there was no way to know exactly what was needed. With generics, however, you can specify that you are comparable with respect to a particular class, and then write a more specific compareTo method that only takes that class as a parameter.

The benefits here are two-fold. Firstly, you don't need to do an instanceof check and a cast in your method's implementation. Secondly, the compiler can do a lot more type checking at compile time - you can't accidentally pass a String into something that implements Comparable<BigInteger>, since the types don't match. It's much better for the compiler to be able to point this out to you, rather than have this cause a runtime exception as would have generally happened in non-generic code.



Related Topics



Leave a reply



Submit