In Java, the arrow operator “->” is a relatively new addition that was introduced in Java 8 as part of the lambda expressions feature. It is used to separate the parameter list from the body of a lambda expression, indicating that the parameters are being passed to the body of the expression. The arrow operator can also be used in method references to refer to a method.
Lambda expressions were introduced in Java 8 to provide a more concise and efficient way to implement functional interfaces. They allow you to pass behavior as an argument to a method, and are useful when you want to perform an action on a collection of objects. The arrow operator is used to separate the parameters from the body of the lambda expression, making it easier to read and write code.
Syntax of Arrow Operator
The arrow operator has the following syntax:
(parameter-list) -> expression
where:
Parameter-list: This is a comma-separated list of parameters that are passed to the lambda expression. If there are no parameters, you can use an empty parameter list “()”.
Arrow operator: This is represented by the “->” symbol, and it separates the parameter list from the body of the lambda expression.
Expression: This is the body of the lambda expression, and it can be a single statement or a block of statements enclosed in curly braces.
Example of Arrow Operator
Here is an example of how the arrow operator is used in a lambda expression:
List names = Arrays.asList("John", "Mary", "Tom");
names.forEach(name -> System.out.println(name));
In this example, we have a list of names, and we are using the forEach method to loop through the list and print each name. The lambda expression “name -> System.out.println(name)” is passed as an argument to the forEach method. The parameter “name” represents each element of the list, and the expression “System.out.println(name)” is the body of the lambda expression.
Arrow Operator in Method References
The arrow operator can also be used in method references. A method reference is a shorthand notation for a lambda expression that calls a method. It is represented by the double colon “::” symbol. Here is an example of how the arrow operator is used in a method reference:
List names = Arrays.asList("John", "Mary", "Tom");
names.forEach(System.out::println);
In this example, we are using the forEach method to loop through the list and print each name. Instead of passing a lambda expression as an argument, we are passing a method reference to the println method of the System.out object. The arrow operator separates the System.out object from the println method.
Conclusion
The arrow operator “->” is a useful addition to the Java language that makes it easier to write lambda expressions and method references. It provides a clear separation between the parameter list and the body of the lambda expression, making the code more readable and concise. The arrow operator is a powerful tool for working with collections of objects, and it is widely used in modern Java programming.
Related Topics
convert int to string in java difference between c++ and java difference between jdk jre and jvm how to install java inheritance in java install java instanceof java instanceof operator in java instanceof operator java int to string in java int to string java java substring jdk jre jvm string to int java substring in java super keyword java