What is Java syntax, where is it like a method, parameterized, mean?

Here is the code I'm viewing (using Google ImmutableMap)

ImmutableMap.<String,String>of(); 

What does it mean? What value does

 Class.<GenericType>methodName()? 
+4
source share
2 answers

ImmutableMap is a common class with two parameters of type K and V This syntax gives specific values ​​for two parameters, in this case String .

So the above returns an empty map from String to String .

See JLS 15.2 , which, among other things, says the invokation method is

  MethodInvocation:
      TypeName.  NonWildTypeArguments Identifier (ArgumentListopt)

Here are arguments of type String and String

+5
source

He used to indicate generic type (s) when type inference doesn't work, for example:

 Map<String, String> m = ImmutableMap.of(); Map<String, String> m2 = ImmutableMap.<String, String>builder().build(); 

It parameterizes the general return type of the method.

+3
source

Source: https://habr.com/ru/post/1437694/


All Articles