Java Lambda Default Functional Interface

Java Lambda 기본 함수형 인터페이스 java.util.function 패키지에 정의되어 있음.

    • Functions
      public interface Function<T, R> {
        
        R apply(T t);
      }
    • Suppliers
      public interface Supplier<T> {
        T get();
      }
    • Consumers
      public interface Consumer<T> {
      
        void accept(T t); 
      }
    • Predicates
      public interface Predicate<T> {
      
        boolean test(T t); 
      }
    • Operators
      public interface UnaryOperator<T> extends Function<T, T> {
        static <T> UnaryOperator<T> identity() {
          return t -> t;
        }
      }
      
      public interface BinaryOperator<T> extends BiFunction<T,T,T> {
      
        public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
          Objects.requireNonNull(comparator);
          return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
        }
      
        public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
          Objects.requireNonNull(comparator);
          return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
        }
      }
       

First-class function, High-order function, Closure

First-class function (일급함수)

It treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.

일급함수란 프로그래밍 언어에서 함수를 값으로 다룰 수 있는 것(즉, 함수 스스로 객체 취급)으로, 함수를 변수에 담거나 데이터 구조 안에 저장하고 원할 때 평가 가능, 따라서 변수의 특성인 함수를 “인자“로 전달 가능하며, “리턴 값“으로 사용 가능하다.

High-order function(고차함수)

A higher-order function is a function that accepts other functions as parameters and/or use a function as the return value.

고차함수란 함수를 인자로 전달받거나 and/or 함수를 결과로 반환하는 함수이다.

Closure (클로저)

closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions.  Operationally, a closure is a record storing a function[a] together with an environment.[1] The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created.[b] Unlike a plain function, a closure allows the function to access those captured variables through the closure’s copies of their values or references, even when the function is invoked outside their scope.

클로저는 자유변수(free variables)에 엮여있는 함수.  클로저는 일반 함수와 다르게, 자신의 영역 밖에서 호출된 함수의 변수값과 레퍼런스를 복사, 저장, 접근을 가능하게 한다. (즉, 외부 범위의 변수를 함수 내부로 바인딩하는 기술)

 

String vs String Literal vs StringBuilder

// String Literal uses String common pool.
// String is immutable object.
// StringBuilder is mutable object.

// + operator (new StringBuilder(String.valueOf(str1)).append(str2).toString();
String str1 = "P";
String str2 = "P";
String str3 = str1 + str2;

// str1.concat(str2) method creates new String
String str4 = str1.concat(str2);

Autoboxing


Integer a = 1000, b = 1000;
System.out.println(a == b); // false

// WARNING: JVM tries to save memory
// when the Integer falls in a range (from -128 to 127).
Integer c = 100, d = 100;
System.out.println(c == d); // true