Studying Java 8 (see method)

This time we are studying method reference. It seems that you can assign a method to a variable of functional IF. I think it's an image that says to execute this assignment (specified) method when the (only) abstract method of functional IF is called. For those who have forgotten the functional IF see previous article

For the time being, prepare two functional IFs as shown below.

public interface SampleFunctionalIF {

	String methodA();

public interface SampleFunctionalIF2 {

	String methodA(String A, String B);


So, here is a class that calls these

 *Study method reference.
 *You can assign a method to a variable of functional IF.
 *Functional IF(I think it's an image of telling you to execute this assignment (specified) method when the (only) abstract method is called.
 * @author komikcomik
public class MethodRef {

	public static void main(String[] args) {
		/*With no arguments*/
		//An image that asks you to execute the method hello of this class instead when methodA, which is an abstract IF of SampleFunctionalIF, is called.
		System.out.println("-----Example 1-----");
		SampleFunctionalIF if1 = MethodRef::hello;

		/*With one argument*/
		//This area is just priced appropriately in the list
		System.out.println("-----Example 2-----");
		List<String> l = new ArrayList<>();
		l.forEach(System.out::println); //System implementation of abstract method of List which is functional IF.out.println(s)Image specified as
		l.forEach(MethodRef::echo); //It's similar to the one above, but of course you can substitute your own method

		/*With 2 arguments*/
		System.out.println("-----Example 3-----");
		SampleFunctionalIF2 if2 = MethodRef::join;
		System.out.println(if2.methodA("Hello", "World"));


	public static String hello() {
		System.out.println("The hello method was called");
		return "hello";


	public static String join(String A, String B) {
		return A + B;


	public static void echo(String s) {

The execution result is as follows.

-----Example 1-----
The hello method was called
-----Example 2-----
-----Example 3-----

Well as expected. However, I'm not used to writing lambda expressions or writing like :: this time, so I somehow get ready. The lambda expression is written as argument-> processing </ b>, and if you write the equivalent of this example 2 in the lambda expression

		l.forEach(s -> {

So it seems that a similar effect can be obtained. How do people who use it properly use it properly?

Recommended Posts

Studying Java 8 (see method)
Studying Java 8 (see constructor)
Studying Java ―― 3
Studying Java ―― 9
Java method
java (method)
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Java method
Studying Java ②
[Java] method
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
[Java] method
Java8 method reference
Studying Java 8 (Optional)
[Java] forEach method
Studying java9 (jShell)
Studying Java 8 (Stream)
java8 method reference
[Java] Random method
[Java] split method
JAVA DB connection method
Studying Java 8 (Collector / Collectors)
Java learning 2 (learning calculation method)
Java learning memo (method)
About Java method binding
About method splitting (Java)
Java programming (class method)
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
[Java] Basic method notes
[Java] New Thread generation method (2)
Java GC method determination conditions
Java Silver Study Method Memo
Create a java method [Memo] [java11]
Java test code method collection
[Java Silver] About equals method
[Java] Timer processing implementation method
[Java] Random number generation method (Random)
Java methods and method overloads
Benefits of Java static method
[Java Silver] Array generation method
[Java] New Thread generation method (1)
[Java] Object-oriented syntax --class method / argument
Automatic photo resizing method in Java
Java method list (memorial) (under construction)
[Java] How to use join method
Screen transition by Post method [Java]
[Java] Object-oriented syntax-class / field / method / scope
Java comparison using the compareTo () method
[Java beginner] == operator and equals method
Studying Java 8 (StaticIF and Default methods)
Try to extract java public method