Java 8 - Primitive Type Functional Interface Understanding

Primitive Type Functional Interface:

Autoboxing: (After Java v.1.5)
Converting primitive data type to object type automatically by compiler, this is called Autoboxing.
Eg. Integer i=10; => Integer i = Integer.valueOf(10); (Internally this will happend)

Auto Unboxing:
Converting object type to primitive data type automatically by compiler, this is called Auto Unboxing.
Eg. Integer I = new Integer(10);
int x=I; (After Java v.1.5)

Generics Type Parameter:
ArrayList<Integer> al = new ArrayList<Integer>();
Integer - type parameter
ArrayList<int> al = new ArrayList<int>(); - Not Valid
For the type parameter only object types are allowed. Primitives not allowed to pass.

Primitive Type Functional Interface for Predict:
Drawback program:
Eg. Print Even numbers
1. int[] = {0,5,10,15,20,25,30};
2. Predicate<Integer> p = i -> i%2==0; // Step2:i%2 can't happend with Object value, So it has to convert into primitive (Auto Unboxing)
3. for(int x1: x){ 
4. if(p.test(x1)){   // Step1: Here x1 to line (2) i -> Converting value into Object (Autoboxing). 
5. System.out.println(x1);
6. }
7. }

Because of this autoboxing and auto unboxing performance will be very slow. So they introduced primitive type functional interface. So that input also and output also we can use primitive type.

There are various primitive functional interfaces are there for Predicate, Function, Consumer and Supplier.
Lets see those in seperate Topic.



Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab