Signup/Sign In

Java 8 Improved Type Inference

Type inference is an ability of the compiler to infer the type of parameters at the time of generic method invocation. In other words, we can say that type Inference is the process of automatically detecting unspecified data types of an expression based on the contextual information.

In Java 8, type inference is improved and provides the ability to invoke the generic type methods without specifying the type. It is used in collection API where we have to specify the type of elements while creating a new data structure.

Now, we can invoke the same generic types and methods without specifying the parameter types. The compiler automatically infers the parameter types when needed.

The purpose of using type inference is to reduce unnecessary code verbosity.

Time for an Example:

Let's take an example to understand the type inference. Here, we used two declarations, first used Java 6 or early version, and second used Java 7 version. We can notice that in the second declaration we used empty diamond(<>) operators that show type inference capability of Java compiler.

import java.util.ArrayList;
import java.util.List;

public class STDemo {
	public static void main(String[] args) {
		// Java earlier versions
		List<Integer> numbers = new ArrayList<Integer>();
		numbers.add(100);
		numbers.add(101);
		System.out.println(numbers);
		// Java 7 version
		numbers = new ArrayList<>();
		numbers.add(102);
		numbers.add(103);
		System.out.println(numbers);
	}
}


[100, 101]
[102, 103]

Example: Java 8 Improved Type inference

In Java 8, type inference is improved so that we can call generic methods without specifying the type. See the below example.

import java.util.ArrayList;
import java.util.List;

public class STDemo {
	public static void main(String[] args) {
		addMore(new ArrayList<>(), 12, 12);
	}
	
	static void addMore(List<Integer> list, int a, int b) {
		list.add(a);
		list.add(b);
		System.out.println(list);
	}
}


[12, 12]

Example:

In this example, we added array elements to a list without specifying the type of elements that shows the type inference power of Java 8. Here, we used two statements first used Java 7 syntax to add array elements while the second used Java 8. Notice, we have to mention the type of elements in Java 7 whereas not in case Java 8.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class STDemo {
	public static void main(String[] args) {
		List<Integer> numbers = new ArrayList<>();
		numbers.add(100);
		numbers.add(101);
		numbers.add(102);
		numbers.add(103);
		System.out.println(numbers);
		// Java 7
		numbers.addAll(Arrays.<Integer>asList(104));
		System.out.println(numbers);
		// Java 8
		numbers.addAll(Arrays.asList(105));
		System.out.println(numbers);
	}
}


[100, 101, 102, 103]
[100, 101, 102, 103, 104]
[100, 101, 102, 103, 104, 105]

Example: Type inference in Lambda Expression

Let's take one more example where we are using a lambda expression to print the list elements. We used the forEach() method and in a lambda expression, we did not mention the type of elements, this is due to improved type inference.

import java.util.ArrayList;
import java.util.List;

public class STDemo {
	public static void main(String[] args) {
		// Java earlier versions
		List<Integer> numbers = new ArrayList<>();
		numbers.add(100);
		numbers.add(101);
		numbers.add(102);
		numbers.add(103);
		// Java 8 versions
		numbers.forEach(element->System.out.println(element));
	}
}


100
101
102
103



About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.