Wednesday, 15 May 2013

java - Why can not I assign method reference directly to variable of Object type? -


Java-8 Simple questions about syntax. Why does this restrict the expressions like JLS-8 :

  object at_on = stream: why; // compile-time error  

and allow only a few:

  java.util.function.function of_ref = stream :: of; Object obj = of_ref; // compiles ok  

?

This is because the goal type should be a method reference or a functional interface of lambda expression. Based on that only, Runtime will make an example of a class for the implementation of given functional interface. Think of Lambda or method references as a concept, assigning it a functional interface type, it gives a solid meaning.

In addition, there may be several functional interfaces in the form of a particular lambda or method reference, its target type. For example, consider the following Lambda:

  int x = 5; Functional interface func = (x) - & gt; Println (x);  

This lambda is a consumer of x . In addition, any interface with an abstract method with the following signature:

  public abstract zero xxx (int value);  

can be used as a target type, so if you specify the type of lambda to be the object , then which interface you run the runtime Want to? This is why you have to explicitly provide a functional interface in the form of goal type.

Now, once you have found a frequency functional interface reference, you can call it any super reference ( Object )


No comments:

Post a Comment