문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
java:7 [2011/09/07 19:11] kwon37xi |
java:7 [2016/01/27 22:58] kwon37xi |
||
---|---|---|---|
줄 9: | 줄 9: | ||
</ | </ | ||
바(|)를 통해 여러가지 예외를 한번에 catch 할 수 있다. | 바(|)를 통해 여러가지 예외를 한번에 catch 할 수 있다. | ||
- | |||
- | * 참고 : 모든 Reflection 관련 예외들은 [[http:// | ||
===== try with resource ===== | ===== try with resource ===== | ||
줄 45: | 줄 43: | ||
System.out.println(line); | System.out.println(line); | ||
line = reader.readLine(); | line = reader.readLine(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | try () 안에서는 세미콜론으로 구분하여 여러개의 리소스를 생성해도 된다. | ||
+ | ===== 예외 다시 던질 때 예외 클래스 자동 판단 ===== | ||
+ | catch 절에서 예외를 잡아서 다시 던질 때, Java 7 이전에는 catch 절에서 잡는 타입에 따라 해당 메소드의 throws 절이 결정되었었다. | ||
+ | <code java> | ||
+ | public static class FirstException extends Exception { | ||
+ | } | ||
+ | |||
+ | public static class SecondException extends Exception { | ||
+ | } | ||
+ | |||
+ | public static void rethrowException(String msg) throws Exception { | ||
+ | try { | ||
+ | switch (msg) { | ||
+ | case " | ||
+ | throw new FirstException(); | ||
+ | default: | ||
+ | throw new SecondException(); | ||
+ | } | ||
+ | } catch (Exception ex) { | ||
+ | throw ex; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 즉, rethrowException 메소드는 **catch (Exception ex)** 때문에 항상 **throws Exception**으로 선언되어야 했었다. 하지만 Java 7 부터는 try 블록에서 실제로 발생 가능한 예외를 컴파일러가 분석하여, | ||
+ | 다음과 같이 '' | ||
+ | <code java> | ||
+ | public static void rethrowException(String msg) throws FirstException, | ||
+ | try { | ||
+ | switch (msg) { | ||
+ | case " | ||
+ | throw new FirstException(); | ||
+ | default: | ||
+ | throw new SecondException(); | ||
+ | } | ||
+ | } catch (Exception ex) { | ||
+ | throw ex; | ||
} | } | ||
} | } | ||
줄 98: | 줄 137: | ||
숫자값에 밑줄(_)을 써도 되며 밑줄은 그냥 무시된다. 숫자의 가독성을 높이는데 사용하면 된다. | 숫자값에 밑줄(_)을 써도 되며 밑줄은 그냥 무시된다. 숫자의 가독성을 높이는데 사용하면 된다. | ||
<code java> | <code java> | ||
- | long number = 123_456_700; | ||
- | System.out.println(number); | ||
- | |||
long creditCardNumber = 1234_5678_9012_3456L; | long creditCardNumber = 1234_5678_9012_3456L; | ||
long socialSecurityNumber = 999_99_9999L; | long socialSecurityNumber = 999_99_9999L; | ||
줄 111: | 줄 147: | ||
</ | </ | ||
+ | ===== java.util.Objects 클래스 ===== | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | ===== java.nio.file.Files 클래스 ===== | ||
+ | * [[http:// | ||
+ | * [[java: | ||
+ | |||
+ | ===== MethodHandles ===== | ||
+ | MethodHandle을 사용하면 메소드를 객체로 전달하는 것이 가능해진다. [[http:// | ||
+ | |||
+ | 아래는 multiply 메소드를 MethodHandle 객체로 만든 뒤에 해당 메소드를 호출하는 것을 보여준다. | ||
+ | <code java> | ||
+ | import java.lang.invoke.MethodHandle; | ||
+ | import java.lang.invoke.MethodHandles; | ||
+ | import java.lang.invoke.MethodHandles.Lookup; | ||
+ | import java.lang.invoke.MethodType; | ||
+ | |||
+ | public class Java7Test { | ||
+ | |||
+ | public static int multiply(int i, int j) { | ||
+ | return i * j; | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) throws Throwable { | ||
+ | Lookup lookup = MethodHandles.lookup(); | ||
+ | |||
+ | MethodHandle multiply = lookup.findStatic(Java7Test.class, | ||
+ | | ||
+ | System.out.println(multiply.invoke(5, | ||
+ | | ||
+ | // 1번 인자로 4를 미리 지정해둔다. | ||
+ | MethodHandle quadruple = MethodHandles.insertArguments(multiply, | ||
+ | System.out.println(quadruple.invoke(5)); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 기타 ===== | ||
+ | * 모든 Reflection 관련 예외들은 [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
===== 참조 ===== | ===== 참조 ===== | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// |