Type inference is used in Java not generics

Here's how Java Tutorials define type inference:

Type inference is the ability of the Java compiler to call each method and the corresponding declaration to determine the type of argument (or arguments) that make the call applicable. the inference algorithm determines the types of arguments and, if available, the type to which the result is assigned or is returned. Finally, the output algorithm tries to find the most specific type that works with all arguments.

Below he explains the type inference using all generic examples.

My questions: Is the type of output in Java only applicable when generic tricks come into play? If not, an example or two will be useful where we can see it?

+5
source share
3 answers

In lambdas you do not need to specify argument types and return types, they are automatically output. For instance:

listOfStrings.stream(). map((aString) -> { //do something anObject anobject = new anObject(); return anobject;}) .collect(Collectors.toList()); 

The return type is List<anObject> , and I did not need to indicate that aString is of type String, as it is inferred by the type of listOfStrings.

+2
source

My first thought was: "not necessary"; as a compiler, since you ever had to analyze the arguments of a method in order to choose a matching method when overloading occurs (for example, if you understand which foo (int) versus foo (long) versus foo (double) should be called for some foo(x) ).

But well, it's about terminology. Therefore, the fact that “some calculations” are necessary to find the correct overloaded method does not mean that “java fathers” believe that these calculations take into account “type inference”.

On the contrary: the Java language specification contains a whole chapter on the type of output ; and when you carefully study this, it is "only" about generic types and lambdas.

So the answers seem to be the following: when Java people use this term, this only applies to generics / lambdas; and not about other situations where argument types need to be analyzed.

+1
source

It would not make sense to have type inference in a true non-gen environment, because otherwise you and the compiler already know the type and should not interfere with the compiler in anything. However, the closure example I could find is Target Types . From the documents

Take a method declared like this

 void processStringList(List<String> stringList) { // process stringList } 

And you call it with

 processStringList(Collections.emptyList()); 

Since Collections.emptyList() returns a List<Object , if no target is specified, it throws an error

 List<Object> cannot be converted to List<String> 

Here, the compiler cannot interfere with the type arguments for List<> . You must explicitly specify the target type, e.g.

 processStringList(Collections.<String>emptyList()); 

However, with JDK 8, this is no longer necessary. Oracle docs Type Inference

This is no longer required in Java SE 8. The concept of what type of destination has been extended to include method arguments, such as the processStringList method argument. In this case, processStringList requires an argument of type List. The Collections.emptyList method returns a List value, therefore, using the target List type, the compiler reports that the argument of type T is String. Thus, in Java SE 8, the following statement compiles:

So, in the last example, the compiler intervenes in the type argument to get the List<String> from Collections.emptyList() .

+1
source

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


All Articles