Lambda: IntUnaryOperator applyAsInt avoids boxing in a numeric transform

IntUnaryOperator takes and returns a primitive int, making it the boxing-free counterpart of UnaryOperator<Integer> for simple numeric transforms.

Code
IntUnaryOperator square = n -> n * n;
System.out.println("square.applyAsInt(6) = " + square.applyAsInt(6));
System.out.println("square.applyAsInt(-3) = " + square.applyAsInt(-3));
Output
square.applyAsInt(6) = 36
square.applyAsInt(-3) = 9
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms