ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] λžŒλ‹€μ‹μ΄λž€?
    Android/Java 2021. 3. 21. 23:06
    λ°˜μ‘ν˜•
    • 이 글은 "μžλ°” 온라인 μŠ€ν„°λ””"μ—μ„œ κ³΅λΆ€ν•œ λ‚΄μš©μ„ μ •λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

    λžŒλ‹€μ‹μ΄λž€?

    • λžŒλ‹€μ‹μ΄λž€ ν•¨μˆ˜λͺ…을 μ„ μ–Έν•˜κ³  μ‚¬μš©ν•˜λŠ” 것이 μ•„λ‹Œ μ‹λ³„μž 없이 μ‹€ν–‰κ°€λŠ₯ν•œ ν•¨μˆ˜μž…λ‹ˆλ‹€.

    • μ ˆμ°¨ν˜• ν”„λ‘œκ·Έλž˜λ°, 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°κ³ΌλŠ” 사뭇 λ‹€λ₯΄κ²Œ ν•¨μˆ˜μ˜ κ΅¬ν˜„κ³Ό 호좜만으둜 ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“œλŠ” 방식인 ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ—μ„œ 자주 μ‚¬μš©λ©λ‹ˆλ‹€.

    • μžλ°” 8λΆ€ν„° λ„μž…μ΄ λ˜μ—ˆμœΌλ©°, λžŒλ‹€μ‹μ„ 읡λͺ…ν•¨μˆ˜λ₯Ό λ§Œλ“œλŠ” 것인데 읡λͺ…ν•¨μˆ˜λŠ” 일급객체 νŠΉμ§•μ„ 가지고 μžˆμŠ΅λ‹ˆλ‹€.

    • λžŒλ‹€μ‹ μ‚¬μš©λ²•μ€ (λ§€κ°œλ³€μˆ˜, ....) -> { μ‹€ν–‰λ¬Έ ... } λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€.


    μΌκΈ‰κ°μ²΄λž€?

    ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ— 1κΈ‰κ°μ²΄λΌλŠ” 말을 자주 μ‚¬μš©ν•˜λŠ”λ°, 1급객체가 무엇인지 μ •λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€. Java와 Kotlin의 비ꡐλ₯Ό 톡해 Kotlin ν•¨μˆ˜λŠ” 1κΈ‰ κ°μ²΄μ§€λ§Œ, JavaλŠ” 1급객체가 μ•„λ‹ˆλΌλŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜μ˜ 3가지 쑰건을 λ§Œμ‘±ν•˜λ©΄ 1급객체라고 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    1. ν•¨μˆ˜λ₯Ό λ³€μˆ˜μ— ν• λ‹Ήν•  수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.
    2. λ‹€λ₯Έ ν•¨μˆ˜μ˜ νŒŒλΌλ―Έν„°λ‘œ ν•¨μˆ˜ 객체λ₯Ό λ„˜κΈΈ 수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.
    3. ν•¨μˆ˜μ˜ λ¦¬ν„΄κ°’μœΌλ‘œ ν•¨μˆ˜ 객체λ₯Ό 리턴할 수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.
    interface OperatorInterface{
        int oper(int a, int b);
    }
    
    public class Hello {
        public static void main(String[] args) {
    
            OperatorInterface operatorInterface = new OperatorInterface() {
                @Override
                public int oper(int a, int b) {
                    return a+b;
                }
            };
            System.out.println(operatorInterface.oper(20,10));      // 30
    
            OperatorInterface operatorInterface1 = ((a, b) -> {
               return a-b;
            });
            System.out.println(operatorInterface1.oper(20,10));     // 10
        }
    }

    λžŒλ‹€μ‹μ€ κ΅¬ν˜„ν•  λ•Œ μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ†Œλ“œλŠ” 두 개 이상 있으면 μ•ˆλ©λ‹ˆλ‹€. μœ„μ˜ μ½”λ“œμ™€ 같이 oper λΌλŠ” λ©”μ†Œλ“œλ₯Ό μΆ”μƒλ©”μ†Œλ“œλ‘œ μ„ μ–Έν•˜κ³  main μ½”λ“œλ‚΄μ—μ„œ μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 첫 번째 operationInterfaceλŠ” λžŒλ‹€μ‹μ„ μ‚¬μš©ν•˜μ§€ μ•Šκ³  λ§μ…ˆμ„ κ΅¬ν˜„ν•˜λ„λ‘ μ •μ˜ν•˜μ˜€μŠ΅λ‹ˆλ‹€. 두 번째 operationInterfaceλŠ” λžŒλ‹€μ‹μ„ μ‚¬μš©ν•˜μ—¬ λΊ„μ…ˆμ„ κ΅¬ν˜„ν•˜μ˜€μŠ΅λ‹ˆλ‹€. λžŒλ‹€μ‹μ˜ μž₯점 쀑 ν•˜λ‚˜μΈ 가독성이 μ’‹μ•„μ§€λŠ” 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.


    ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€

    μ—¬λŸ¬ μΈν„°νŽ˜μ΄μŠ€κ°€ μ‘΄μž¬ν•˜μ—¬ λžŒλ‹€μ‹μœΌλ‘œ μ‚¬μš©ν•  μΈν„°νŽ˜μ΄μŠ€μ§€λ§Œ 두 개 이상을 μ„ μ–Έν•˜λŠ” μ‹€μˆ˜κ°€ μ‘΄μž¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄ ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€μΈ @FunctionalInterface μ–΄λ…Έν…Œμ΄μ…˜μ„ λΆ™μ΄κ²Œ 되면 컴파일 νƒ€μž„μ— μ‹€μˆ˜λ₯Ό 방지할 수 μžˆμŠ΅λ‹ˆλ‹€.

    @FunctionalInterface
    interface OperatorInterface{
        int oper(int a, int b);
    }

    Variable Capture

    λ‹€μŒ μ½”λ“œμ—μ„œ λžŒλ‹€μ‹ 외뢀에 μ„ μ–Έλœ λ³€μˆ˜ num을 μžμœ λ³€μˆ˜λΌκ³  ν•©λ‹ˆλ‹€. 이 μžμœ λ³€μˆ˜λ₯Ό λžŒλ‹€μ‹ λ‚΄λΆ€μ—μ„œ λ³€κ²½ν•˜κ²Œ 되면 컴파일 μ—λŸ¬κ°€ λ°œμƒν•˜κ²Œ λ©λ‹ˆλ‹€. 였λ₯˜ λ©”μ‹œμ§€μ—μ„œλŠ” λžŒλ‹€μ‹μ—μ„œ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜λŠ” finalλ³€μˆ˜λ‚˜ effectively finalλ³€μˆ˜μ—¬μ•Όν•œλ‹€κ³  λ‚˜μ™€μžˆμŠ΅λ‹ˆλ‹€. final λ³€μˆ˜λŠ” μ„ μ–Έκ³Ό λ™μ‹œμ— μ΄ˆκΈ°ν™”λ₯Ό ν•œ ν›„ λ³€κ²½ν•  수 μ—†λŠ” λ³€μˆ˜λ₯Ό λ§ν•˜κ³  effectively final λ³€μˆ˜λŠ” μž¬ν• λ‹Ήμ„ ν•˜μ§€ μ•ŠλŠ” λ³€μˆ˜λ₯Ό λœ»ν•©λ‹ˆλ‹€. 이처럼 μžμœ λ³€μˆ˜λ₯Ό λžŒλ‹€μ‹ λ§€κ°œλ³€μˆ˜κ°€ μ•„λ‹Œ λ‚΄λΆ€μ—μ„œ μ°Έμ‘°ν•˜λŠ” ν–‰μœ„λ₯Ό λžŒλ‹€ 캑처링(Lamda Capturing) λ˜λŠ” λ³€μˆ˜ 캑처링(Variable Capturing) 이라고 ν•©λ‹ˆλ‹€.

    μžμœ λ³€μˆ˜κ°€ final or effectively final이어야 ν•˜λŠ” 이유

    • JVM λ©”λͺ¨λ¦¬ ꡬ쑰에 λŒ€ν•΄ μ•Œμ•„μ•Ό ν•˜λŠ”λ° κ°„λ‹¨ν•˜κ²Œ μ„€λͺ…ν•˜μžλ©΄, static λ³€μˆ˜λŠ” method μ˜μ—­μ— μ €μž₯되고 new와 같이 λ™μ μœΌλ‘œ μƒμ„±ν•˜λŠ” λ³€μˆ˜λŠ” heap μ˜μ—­μ— μ§€μ—­λ³€μˆ˜λŠ” stack μ˜μ—­μ— μƒμ„±λ©λ‹ˆλ‹€.
    • μŠ€νƒ μ˜μ—­μ€ λ‹€λ₯Έ μ˜μ—­κ³Ό 달리 μ“°λ ˆλ“œλ§ˆλ‹€ λ³„λ„μ˜ μŠ€νƒ μ˜μ—­μ— μƒμ„±λ˜κΈ° λ•Œλ¬Έμ—, μ“°λ ˆλ“œλΌλ¦¬ κ³΅μœ κ°€ λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
    • λžŒλ‹€μ‹ λ‚΄λΆ€λŠ” λ³„λ„μ˜ μ“°λ ˆλ“œμ—μ„œ 싀행을 ν•˜κΈ° λ•Œλ¬Έμ— μ§€μ—­λ³€μˆ˜λ₯Ό μ„ μ–Έν•œ μ“°λ ˆλ“œμ—μ„œ μ§€μ—­λ³€μˆ˜λ₯Ό μ°Έμ‘°ν•˜κΈ° μœ„ν•΄ μžμ‹ μ˜ μŠ€νƒ μ˜μ—­μ— λ³΅μ‚¬ν•˜κ²Œ λ©λ‹ˆλ‹€.
    • 만일 λ©€ν‹°μ“°λ ˆλ“œ ν™˜κ²½μ—μ„œ μ—¬λŸ¬ μ“°λ ˆλ“œκ°€ λ™μΌν•œ λžŒλ‹€μ‹μ„ μ‚¬μš©ν•œλ‹€κ³  κ°€μ •ν•˜λ©΄, μ—¬λŸ¬ μ“°λ ˆλ“œμ—μ„œ λžŒλ‹€ μΊ‘μ²˜λ§μ€ μ§„ν–‰λ˜μ§€λ§Œ λ³΅μ‚¬λœ μ§€μ—­λ³€μˆ˜λ₯Ό λ³€ν•˜κΈ° λ•Œλ¬Έμ— 동기화 λ¬Έμ œκ°€ λ°œμƒν•˜κ²Œ λ©λ‹ˆλ‹€.
    • μ΄λŸ¬ν•œ 이유둜 λžŒλ‹€μ‹ λ‚΄λΆ€μ—μ„œ μ‚¬μš©ν•˜λŠ” μžμœ λ³€μˆ˜λŠ” finalμ΄κ±°λ‚˜ effectively final 이어야 ν•©λ‹ˆλ‹€.
    • μ§€μ—­λ³€μˆ˜κ°€ μ•„λ‹Œ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ‚˜ μ •μ λ³€μˆ˜λŠ” μ“°λ ˆλ“œλ§ˆλ‹€ λ³„λ„μ˜ μ˜μ—­μ΄ μ•„λ‹Œ κ³΅μœ μ˜μ—­μ΄κΈ° λ•Œλ¬Έμ— λžŒλ‹€μ‹μ—μ„œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    λ©”μ†Œλ“œ μ°Έμ‘°(Method Reference)

    λ©”μ†Œλ“œ μ°Έμ‘°λŠ” λžŒλ‹€μ‹μ„ 더 κ°„νŽΈν•˜κ²Œ μ‚¬μš©ν•˜λŠ” λ°©λ²•μœΌλ‘œ λΆˆν•„μš”ν•œ λ§€κ°œλ³€μˆ˜λ₯Ό μ œκ±°ν•˜κ³  μ‚¬μš©ν•˜λŠ” λ°©λ²•μž…λ‹ˆλ‹€.

    @FunctionalInterface
    interface MathInterface{
        int oper(int n);
    }
    
    public class Hello {
        public static void main(String[] args) {
            MathInterface mathInterface;
            mathInterface = n -> Math.abs(n);
            System.out.println(mathInterface.oper(-3));        // 3
    
            mathInterface = Math::abs;
            System.out.println(mathInterface.oper(-5));        // 5
        }
    }
    • MathInterfaceμ—μ„œ 처음으둜 μ •μ˜ν•œ 뢀뢄은 λžŒλ‹€μ‹μœΌλ‘œ ν‘œν˜„ν•˜μ˜€λŠ”λ°, λ§€κ°œλΆ€λΆ„μ„ μ§€μ›Œμ„œ λ©”μ†Œλ“œ μ°Έμ‘°λ°©μ‹μœΌλ‘œ 두 번째처럼 λ‚˜νƒ€λ‚Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
    • :: 기호λ₯Ό 톡해 κ°„λ‹¨ν•˜κ²Œ ν‘œν˜„ν•  수 있고, νŠΉμ • μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œλ„ μ°Έμ‘°λ³€μˆ˜ 이름을 ν†΅ν•΄μ„œ λ©”μ†Œλ“œ μ°Έμ‘°λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    μƒμ„±μž μ°Έμ‘°(Constructor Reference)

    λ©”μ†Œλ“œ 참쑰와 λΉ„μŠ·ν•œ λ°©λ²•μœΌλ‘œ μƒμ„±μž λžŒλ‹€μ‹μ„ κ°„νŽΈν•˜κ²Œ μ‚¬μš©ν•˜λŠ” λ°©λ²•μž…λ‹ˆλ‹€.

    import java.util.function.Function;
    
    class Student{
        String name;
    
        public Student(String name) {
            this.name = name;
        }
    }
    
    public class Hello {
        public static void main(String[] args) {
    
            Student student = new Student("kim");
            System.out.println(student.name);
    
            Function<String, Student> function = Student::new;
            Student student1 = function.apply("oh");
            System.out.println(student1.name);
        }
    
    }
    λ°˜μ‘ν˜•

    'Android > Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

    [Java] λ””μžμΈ νŒ¨ν„΄(Proxy νŒ¨ν„΄)  (0) 2021.05.14
    [Java] λ””μžμΈ νŒ¨ν„΄(Strategy νŒ¨ν„΄)  (0) 2021.05.11
    [Java] Java Generic(μ œλ„€λ¦­)  (0) 2021.03.01
    [Java] Java IO, NIO  (0) 2021.02.22
    [Java] Annotation, Meta dataλž€?  (0) 2021.02.19
Designed by Tistory.