Hello, Java coder. In this lesson, I'll explain why the double colon (::) operator is used in Java.
The method reference operator in Java is denoted by the double colon (::) operator. It offers a means to refer to constructors or methods without actually calling them and was first introduced in Java 8. The shorthand form for calling a process with a lambda expression is a method reference. They are a component of the Java Stream API and functional programming capabilities, making code easier to read.
What is the Double Colon (::) Operator?
To simply refer to methods or constructors, use the double colon (::) operator. To do the same thing, you can use the double colon operator in place of creating a lambda expression. It is a type of syntactic sugar that improves readability and simplifies the code.
Types of Method References
There are four types of method references:
1. Reference to a Static Method
2. Reference to an Instance Method of a Particular Object
3. Reference to an Instance Method of an Arbitrary Object of a Particular Type
4. Reference to a Constructor
1. Reference to a Static Method
This type of method reference refers to a static method of a class. The syntax is ClassName::staticMethodName.
In this example, Integer::parseInt is a reference to the static method parseInt of the Integer class.
import java.util.function.Supplier;
public class StaticMethodReference
{
public static void main(String[] args)
{
String str = "Hello, World!";
Supplier<String> stringSupplier = str::toUpperCase;
String result = stringSupplier.get();
System.out.println(result); // Outputs: HELLO, WORLD!
}
}
Output:
HELLO, WORLD!
2. Reference to an Instance Method of a Particular Object
This type of method reference refers to an instance method of a particular object. The syntax is object::instanceMethodName.
In this example, str::toUpperCase is a reference to the instance method toUpperCase of the String object str.
import java.util.function.Supplier;
public class InstanceMethodReference
{
public static void main(String[] args)
{
String str = "Hello, World!";
Supplier<String> stringSupplier = str::toUpperCase;
String result = stringSupplier.get();
System.out.println(result); // Outputs: HELLO, WORLD!
}
}
Output:
HELLO, WORLD!
3. Reference to an Instance Method of an Arbitrary Object of a Particular Type
This type of method reference refers to an instance method of an arbitrary object of a particular type. The syntax is ClassName::instanceMethodName.
In this example, String::compareToIgnoreCase is a reference to the instance method compareToIgnoreCase of the String class, applicable to any String object.
import java.util.function.Function;
import java.util.Arrays;
public class ArbitraryObjectMethodReference
{
public static void main(String[] args)
{
String[] stringArray = { "apple", "banana", "cherry" };
Arrays.sort(stringArray, String::compareToIgnoreCase);
for (String str : stringArray) {
System.out.println(str); // Outputs: apple, banana, cherry
}
}
}
Output:
apple
banana
cherry
4. Reference to a Constructor
This type of method reference refers to a constructor.
The syntax is ClassName::new.
In this example, StringBuilder::new is a reference to the StringBuilder constructor that takes a String argument.
import java.util.function.Function;
public class ConstructorReference
{
public static void main(String[] args)
{
Function<String, StringBuilder> stringBuilderFunction = StringBuilder::new;
StringBuilder stringBuilder = stringBuilderFunction.apply("Hello");
System.out.println(stringBuilder.toString()); // Outputs: Hello
}
}
Output:
Hello
Advantages of Using the Double Colon (::) Operator
1. Readability: Method references are more readable and concise compared to lambda expressions.
2. Reusability: They promote code reuse by directly referring to existing methods.
3. Functional Programming: They enhance functional programming capabilities in Java.
4. Maintainability: The code becomes easier to maintain and less prone to errors due to its simplicity.
Conclusion:
The double colon (::) operator is a powerful feature in Java that simplifies code and enhances readability by providing a concise way to refer to methods and constructors. It fits well within the functional programming paradigm introduced in Java 8 and is widely used in the Stream API and other functional interfaces. Understanding and utilizing method references can lead to cleaner, more maintainable, and more expressive Java code.
No comments:
Post a Comment