Java 11 Tutorial
Java Other Versions Tutorials
Java 11 Useful Resources
Selected Reading
- Java 11 - Removed/Deprecated API
- Java 11 - Nest Based Access
- Java 11 - var in lambda
- Java 11 - Not Predicate
- Java 11 - Optional Class
- Java 11 - File APIs
- Java 11 - Collections to Array
- Java 11 - String APIs
- Java 11 - Compile free Launch
- Java 11 - Standard HttpClient
- Java 11 - Environment Setup
- Java 11 - Overview
- Java 11 - Home
Java Other Versions Tutorials
- Java 16 Tutorial
- Java 15 Tutorial
- Java 14 Tutorial
- Java 13 Tutorial
- Java 12 Tutorial
- Java 10 Tutorial
- Java 9 Tutorial
- Java 8 Tutorial
- Java Tutorial
Java 11 Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java 11 - var in lambda
Java 11 - Var in Lambda
Java 11 allows to use var in a lambda expression and it can be used to apply modifiers to local variables.
(@NonNull var value1, @Nullable var value2) -> value1 + value2
Consider the following example −
ApiTester.java
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @interface NonNull {} pubpc class APITester { pubpc static void main(String[] args) { List<String> tutorialsList = Arrays.asList("Java", "HTML"); String tutorials = tutorialsList.stream() .map((@NonNull var tutorial) -> tutorial.toUpperCase()) .collect(Collectors.joining(", ")); System.out.println(tutorials); } }
Output
Java HTML
Limitations
There are certain pmitations on using var in lambda expressions.
var parameters cannot be mixed with other parameters. Following will throw compilation error.
(var v1, v2) -> v1 + v2
var parameters cannot be mixed with other typed parameters. Following will throw compilation error.
(var v1, String v2) -> v1 + v2
var parameters can only be used with parenthesis. Following will throw compilation error.
var v1 -> v1.toLowerCase()Advertisements