반응형

# jdk7의 새로운 기능


[ex] 제너릭의 다이아몬드연산자(<>)를 사용할 때 왼쪽,오른쪽 다 선언해야했으나
 jdk7에서부터 왼쪽만 선언해도 됨.

import java.util.*;
class A{
 public static void main(String args[]){
  Map<String,List<Integer>> map1=new HashMap<String,List<Integer>>();
  Map<String,List<Integer>> map2=new HashMap<>();
 }
}


[ex] jdk6까지 switch문에서 사용할 수 있는 자료형은 byte,char,short,int,enum 만 사용가능.
 jdk7에서는 String형이 추가로 사용가능.

class A{
 public static void main(String args[]){
  String str="Korea";
  switch(str){
   case "Korea" : System.out.println("한국"); break;
   case "America" : System.out.println("미국"); break;
   case "England" : System.out.println("영국"); break;
  }
 }
}

 

[ex] jdk7에서 finally를 사용하지 않아도 자동으로 자원을 회수하는 기능이 추가됨.
 Closeable인터페이스를 구현한 모든 객체에 대해서 자동으로 자원회수기능이 있으니 참고하셈~

import java.io.*;
class A{
 public static void main(String args[]){
  try(B o=new B()){
   o.test();
  }catch(Exception e){
   System.out.println("예외처리");
  }
 }
}
class B implements Closeable{
 public void close(){
  System.out.println("실행");
 }
 void test() throws Exception{
  throw new Exception("폭탄");
 }
}


[ex] jdk7에서는 숫자에 _(밑줄)을 추가할 수 있는 기능이 추가됨.

class A{
 public static void main(String args[]){
  int a=1000;
  int b=1_000;
  int c=1_000_000;

  System.out.println(a+b);
  System.out.println(c);
 }
}


[ex] jdk7에서는 숫자앞에 0b를 선언하면 2진수로 선언됨.
 
class A{
 public static void main(String args[]){
  int a=0b1000;
  int b=0b100;
  System.out.println(a);
  System.out.println(b);
 }
}

 

[ex] jdk7에서 Exception처리가 향상되었다.
 다중catch절을 하나로 줄일 수 있는 기능 추가됨.

class A{
 public static void main(String args[]){
  try{
   Integer.parseInt("a");
   int a=10/0;
  }catch(NumberFormatException e1){
   System.out.println("예외처리1");
  }catch(ArithmeticException e2){
   System.out.println("예외처리2");
  }

  try{
   Integer.parseInt("a");
   int a=10/0;
  }catch(NumberFormatException | ArithmeticException e){
   System.out.println("예외처리");
  }
 }
}

반응형

+ Recent posts