Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the dot operator `.` (before the generic parameter) mean?

I saw this code today:

    ImmutableMap<Class<? extends ClientCommand>, CommandProcessorInterface> immutableMap =
            ImmutableMap.<Class<? extends  ClientCommand>, CommandProcessorInterface>of(...

What does this syntax mean?

ImmutableMap.<Class ..

I knew generics was right after the class name. No?

What is the difference between:

ImmutableMap<Class.. and ImmutableMap.<Class..

like image 370
Elad Benda Avatar asked Nov 27 '14 08:11

Elad Benda


People also ask

What is a dot operator?

(dot) operator is used to access class, structure, or union members. The member is specified by a postfix expression, followed by a . (dot) operator, followed by a possibly qualified identifier or a pseudo-destructor name. (A pseudo-destructor is a destructor of a nonclass type.)

What does the dot operator do in Java?

Simply the dot operator acts as an access provider for objects and classes. The usage of the above operator is as below. It separates a function and variable from an instance variable. It allows accessing sub-packages and classes from a package.

What is a generic type parameter?

Generic Methods A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

What does generic question mark mean Java?

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific).


2 Answers

It means you're invoking a generic static method, called of in the ImmutableMap class.

It's pretty much the same as you're invoking a static method, nested in some class:

SomeClass.staticMethod();

For the cases when your method has a type-parameter defined, you can explicitly provide the generic type and this is done like this:

SomeClass.<Type>genericStaticMethod();

And to answer you final question:

What is the difference between ImmutableMap<Class...> and ImmutableMap.<Class... ?

The first is usually used when creating an instance of a generic class. It's used to define the generic-type on class level, while the second is used to invoke a generic static method that's nested in some class.

like image 189
Konstantin Yovkov Avatar answered Sep 30 '22 20:09

Konstantin Yovkov


It concerns a static method ImmutableMap.of. This has its own generic type parameters.

class Test {

static <T> T f() { return null; }

void t() {
    String s = Test.f();                 // Inferred from LHS.
    String t = Test.<String>f();         // Not needed.
    int n = Test.<String>f().length();   // Needed.
}

In your case it seems not really needed, but there I am on thin ice, as the generic type inference became a bit stronger in Java 8.

like image 22
Joop Eggen Avatar answered Sep 30 '22 19:09

Joop Eggen