함수형 프로그래밍(Functional Programming: FP)
순수함수(pure function)를 구현, 외부변수 사용 X
순수함수(pure function) = 매개변수만을 사용하여 만드는 함수
public class TestMyNumber {
public static void main(String[] args) {
MyNumber max = (x, y)->(x>= y)? x:y;
System.out.println(max.getMax(10, 20));
}
}
자료의 대상과 관계없이 동일한 연산을 수행
사용한 스트림은 재사용 X
기존 자료를 변경 X
중간연산 - 여러 연산 적용 가능 filter(), map(), sorted()
최종연산 - 마지막에 한번만 적용됨 forEach(), count(), sum()
프로그래머가 직접 구현
ex) arr배열의 모든 원소의 합 구하기
Arrays.stream(arr).reduce(0, (a,b)->a+b));
람다식이 길면 BinaryOperator 구현
컴파일 오류(compile error)
실행 오류(runtime error)
시스템 오류(error) : VM에서 발생, 프로그래머가 처리 불가능
예외(Exception) : 프로그램 상에서 제어 가능
Arithmetic Exception :정수를 0 으로 나눈 경우
NullPointerException : 초기화 되지 않은 Object를 사용
ArrayIndexOutOfBoundsException : 배열의 크기를 넘어선 위치를 참조
FileNotFoundException : 참조하는 파일이 지정된 위치에 X
ClassNotFoundException
InterruptedException
try-catch 문
try{
예외가 발생할 수 있는 부분
} catch(처리할 예외타입 e) {
try 블록 안에서 예외가 발생했을 때 예외를 처리하는 부분
}
public class ArrayExceptionHandling {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
try{
for(int i=0; i<=5; i++){
System.out.println(arr[i]);
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
System.out.println("비정상 종료되지 않았습니다.");
}
}
바이트 단위 스트림 : 동영상, 음악 파일, 실행 파일등의 자료를 읽고 쓸 때 사용
문자 단위 스트림 : 바이트 단위로 자료를 처리하면 문자는 깨짐, 인코딩에 맞게 2바이트 이상으로 처리하도록 구현된 스트림