Type Inference in JAVA

Many developers think that type inference is introduced in java in JDK 10 (March, 2018)  release but since JDK 5 java has Type Inference mechanism. The Local type Inference is presented in JDK 10.

First of all, What is Type Inference?

Type inference refers to the automatic detection of the datatype of a variable, done generally at the compiler time.

Example of type inference, JDK 5,

If Type Inference is not applied by the compiler then we have to cast the element into
specific type.Like in example,

List<String> names = new ArrayList<String>();


names.<String>add("Yash");
names.<String>add("Parikh");

System.out.println(names);

But,We don't need to cast because of Type Inference.

List<String> names = new ArrayList<String>();


names.add("Yash");
names.add("Parikh");

System.out.println(names);

After that In JDK 7, Diamond Operator brought into picture.In which the
type parameter may be omitted: you can just leave it to the compiler to
infer the types from the type expression. So the declaration can
be simplified as,

List<String> names = new ArrayList<>();


names.add("Yash");
names.add("Parikh");

System.out.println(names);


In JDK 8,It has Lambda Expressions which uses Type inference.
Example,

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

list.forEach((Integer e) -> System.out.println(e));
list.forEach(e -> System.out.println(e));

For Example, When we create a list of Integers,we know that every element
in list is an integer. So when we loop through all element
using internal for-each loop it is not necessary to explicitly
define the type, compiler infer the type by it self.

But, if anyone want to use any keyword or annotation before that element,
at that time we have to use it's type.

list.forEach((final Integer e) -> System.out.println(e));


list.forEach((final e) -> System.out.println(e));//throws an error

In JDK 10 an alternative approach is introduced.

After that, till JDK 8, We can not create anonymous local inner class when we use the
diamond syntax.But After JDK 9 we can create it.
Example,

List<String> names = new ArrayList<>() {
// local inner class with diamond syntax
{
add("Yash");
add("Parikh");
}
};


System.out.println(names);

In JDK 10, Major use for type inference is presented as Local variable
type inference.For that context-sensitive identifier var is Introduced.
In Java, when we initialize a variable the value must match the type of
that variable. But in this approach the type is inferred by the compiler
at compile type.

Example,

var greet = "yash";
System.out.print(greet);

The type of greet will be String.

After the initialization we cannot assign different type value to that variable.

greet = 1;
//Error
error: incompatible types: int cannot be converted to String
greet = 1;

Keep in mind that var is not a keyword.

Example,

var var = 10;
System.err.println(var);

Another example, with List,

List<String> name = Arrays.asList("yash", "parikh");


for(var string: name){
System.out.println(string);
}

We cannot use this var identifier to declare instance variable, return type or
as a parameter type.

public void foo(var a){} //ERROR

In JDK 11,we can use var variable with lambda expression to use keywords
or annotation.

Example,

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
list.forEach((final var e) -> System.out.println(e));

Thus, This is the use of Type inference in JAVA till JDK 11. It is used
in many other programming languages like Scala, Swift etc. and C# & C++
is also moving towards it.

Comments

Popular posts from this blog

The Battle Of Tiger Hill, Kargil War, 1999

Atal Bihari Bajpai.....10 Untold Facts