In this blog post, I will be explaining how the Java 8 functional interface IntConsumer works. To know more about functional interfaces, you can refer this blog post.
The IntConsumer interface provides a method called accept. It accepts a single parameter of int data type. It does not return anything, it returns a void. So it operates via side effects i.e. it modifies the parameter passed in. The IntConsumer interface is a specialization of the Consumer interface. While the Consumer interface accepts any data type, the IntConsumer interface accepts an Integer value.
IntConsumer Example
Consider the following code snippet:
public class IntConsumerDemo {
public static void main(String args[]) {
IntConsumer incrementby5 = (num) -> System.out.println("Input:"+num+", Incremented Value:"+(num+5));
incrementby5.accept(12);
incrementby5.accept(23);
}
}
Here, the IntConsumer.accept method accepts an integer value. It increments the input by 5 and prints the result. So when the above code is executed, it will print the following output:
Input:12, Incremented Value:17
Input:23, Incremented Value:28
You can get the source code for this example along with other code for other Java 8 examples at the GitHub repository here.