Home > Java > javaTutorial > Java Type Inference

Java Type Inference

WBOY
Release: 2024-08-30 15:17:51
Original
965 people have browsed it

Type inference in java is a compiler process that automatically infers unspecified data types parameter from the contextual information. Consider an example where we want to create an object of type generic class. So to create this object we need to call the construct of a generic class with the specified type of parameters, like String, Float, Integer etc., which increase the length of code. To reduce this coding java provides the flexibility to learn the type parameter empty as long as the compiler judge or guess the type of parameter from the context. In addition to this java also provide the wildcards that allow the user to achieve the inheritance in a type parameter. Java 8 provides an improved version of type inference. In case of type, the inference is not used then the compiler generates an unchecked conversion warning.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

Syntax are as follow:

Generic_class < type_parameter > obj = new Generic_class<> ( );
Where -
Generic_class – Generic_class  is an  user create generic class.
<type_parameter> - type_parameter is a type parameter, which represented by agular brakets(<>), that can have one or more type of parameter separated by commas.
Obj – obj is the object of the generic class.
< > − < > (diamond) represents the type inference.
Copy after login

Examples to Implement Java Type Inference

Next, we write the java code to understand this more clearly with the following example where we create a generic class to accept the pair of values by using the generic class constructor and create the objects of the generic class for different pair of data types and then use the type inference for the type parameters, as below –

Example #1

Code:

package p1;
class Pair <x, y>
{
private x first;
private y second;
public Pair(x a, y b)
{
first=a;
second=b;
}
public x getFirst() {
return first;
}
public y getSecond() {
return second;
}
}
public class Demo
{
public static void main( String[] arg)
{
// unchecked conversion warning
Pair <Integer, String> ob1 = new Pair<Integer, String>(25, "Test1");
System.out.println("Integer value is : "+ob1.getFirst());
System.out.println("String valueis : "+ob1.getSecond());
System.out.println( );
// unchecked conversion warning
Pair <String, Integer> ob2 = new Pair<String, Integer>("Test2", 30);
System.out.println("String valueis : "+ob2.getFirst());
System.out.println("Integer is : "+ob2.getSecond());
System.out.println( );
// type inference, < > left it blank, compiler will infer type
Pair <String, Integer> ob3 = new Pair<String, Integer>("Test3", 30);
System.out.println("Integer valueis : "+ob3.getFirst());
System.out.println("Integer value is : "+ob3.getSecond());
System.out.println( );
// type inference, < > left it blank, compiler will infer type
Pair <Integer, Integer> ob4 = new Pair< >(35, 40);
System.out.println("Integer value is : "+ob4.getFirst());
System.out.println("Integer value is : "+ob4.getSecond());
System.out.println( );
}
}
Copy after login

Output:

Java Type Inference

Explanation: As in the above code, the generic class Pair can two different data types in the class as x and y. Here, the first two objects creating by mentioning integer and /or String type explicitly at both sides, which are the earlier versions of Java. And in the last two objects creating examples the types are mentioning at one side(we can be left <> second side as blank), which was introduced in Java 7. And later introduce to call a specified method without explicitly mentioning of type of arguments in java 8, which we will see in the next example.

Example #2

Next, we write the java code to understand the new type inference where we create a generic class to accept the different type of message by using the generic class of setter method and create the objects of the generic class for the different message of different data types and then use the type inference for the type parameters, as below:

Code:

package demo;
class myGeneric < x >
{
private x msg;
public x getMsg() {
return msg;
}
public void setMsg(x msg) {
this.msg = msg;
}
public String genInf1(myGeneric <String> m){
m.setMsg( "This is a Hello Message." );
return m.msg;
}
public Integer genInf2(myGeneric <Integer> m){
m.setMsg( 100 );;
return m.msg;
}
}
public class Demo {
public static void main(String[] args) {
// Before java 7 an old approach to create generic class object
myGeneric <String> msg1 = new myGeneric <String>();
msg1.setMsg( "This is a first Message.");
System.out.println(msg1.getMsg());
// type inference
myGeneric <Integer> msg2 = new myGeneric <>();
msg2.setMsg(20);
System.out.println(msg2.getMsg());
// New type inference
System.out.println(msg1.genInf1( new myGeneric<>() ));
System.out.println(msg1.genInf2( new myGeneric<>() ));
System.out.println(msg2.genInf1( new myGeneric<>() ));
System.out.println(msg2.genInf2( new myGeneric<>() ));
}
}
Copy after login

Output:

Java Type Inference

Explanation: As in the above code, calling a specific(genInf1() and genInf2()) methods without explicitly mentioning of type of arguments which are the example of new type inference introduce in java 8.

Example #3

Next, we write the code to understand the new type inference where we create a list of the message by using the List generic class and create the objects of the list generic class for the different message of different data types and then use the type inference and new type inference for the type parameters, as below:

Code:

package demo;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main( String[] arg) {
// Before Java 7 to create a list
List <String> msg1 = new ArrayList <String>();
msg1.add("This is a first Message.");
getMsg(msg1);
// Java 7 type inference
List<String> msg2 = new ArrayList<>();
msg2.add("This is a second Message.");
getMsg(msg2);
// as list is generic class so add integer
List<Integer> msg3 = new ArrayList<>();
msg3.add(100);
getMsg1(msg3);
// Java 8 Compiler type infers type of ArrayList
getMsg(new ArrayList<>());
}
public static void getMsg(List <String> m){
if(!m.isEmpty()){
m.forEach(System.out::println);
}else System.out.println("No Message.");
}
public static void getMsg1(List <Integer> m){
if(!m.isEmpty()){
m.forEach(System.out::println);
}else System.out.println("No Message.");
}
}
Copy after login

Output:

Java Type Inference

Conclusion

Type inference is used to create an object of type generic class and if we want compiler automatically to infer unspecified data types parameter from the context pass.

The above is the detailed content of Java Type Inference. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template