JAVA 7文法新特性

4225 ワード

color=red 1、switch文で文字列タイプを使用可能
次の例を見てください

public class SwitchSamlpe {
	
	public static void main(String[] args){
		SwitchSamlpe.show("man");
		SwitchSamlpe.show("\u006d\u0061\u006e");
	}
	
	public static void show(String gender){
		switch(gender){
		case "man":
		    System.out.println("hi guy");
		    break;
		case "woman":
		    System.out.println("hi beauty");
		    break;
		default:
			System.out.println("hi	");
		}
	}

}

実行後にhiguyを2つ同時に打つと,unicode符号化を直接用いることで多くの誤解が生じることがわかる.この実現の核心もstringを対応するハッシュ値に変換しただけだ.
2、バイナリに新しい表現を追加する

public class LiteralSamlpe {
	public static void main(String[] args){
		System.out.println(0b10011);
		System.out.println(0B10011);
	}
}

OBとObを増やして2進数を表す
3、数値は下線で表すことができます

public class LiteralSamlpe {
	public static void main(String[] args){
		System.out.println(150_000_000);
	}
}

下線を引いて表示するために15,000,000,000,000を出力します.
4、java 7の異常に対する処理
まず以下の例を見てみましょう

public class ExceptionSample {
	@SuppressWarnings("finally")
	public static void main(String[] args){
		try{
			throw new IOException("main exception");
		}catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}finally{
			try {
				throw new IOException("sub exception");
			} catch (IOException e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}

}

実行後はsub exceptionという異常しか表示されません.それはtryにおける主な異常が後のfinallyにおけるsub異常に上書きされ,プログラマの判断ミスを招くためである.
JAva 7はaddSuppressedメソッドを提供してこの問題を解決します

public class ExceptionSampleJava7 {
	public static void main(String[] args) {
		RuntimeException e1 = null;
		try{
			throw new IOException("main exception");
		}catch (Exception e) {
			 e1=  new  RuntimeException(e.getMessage()); 
			throw e1;
		}finally{
			try {
				throw new IOException("sub exception");
			} catch (final IOException e) {
				e1.addSuppressed(e);
			}
		}
	}
}

Exception in thread "main"java.lang.RuntimeException: main exception at com.xxx.java7.ExceptionSampleJava7.main(ExceptionSampleJava7.java:11) Suppressed: java.io.IOException: sub exception at com.xxx.java7.ExceptionSampleJava7.main(ExceptionSampleJava7.java:15)
以上のコードを実行するとmain異常が上書きされていないだけでなくsuppressedされた異常も明確に記録されていることがわかります.
5、java 7は1つのcatchが複数の異常をキャプチャすることを許可する

public class ExceptionSampleJava7 {
	public static void main(String[] args) {
		try {
			throw new IOException("exception");
		} catch (IOException | RuntimeException e) {
			e.getStackTrace();
		}
	}
}


これにより、コードの複雑さを大幅に低減できます.ただし、前の異常は後の異常より小さくなければならないことに注意してください.
6、try-with-resourcesの伝統的なIO操作を増加して、私達はすべて対流に対して閉じることができます
次のように

public class TryWithSample {
	@SuppressWarnings("unused")
	private static void read(File source) {
	    InputStream fis = null;
	    try {
	        fis = new FileInputStream(source);
	    }
	    catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	    	try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }
	}
}


これにより、コードの上で非常に美しくなく、複雑さが増加します.
JAva 7はこのような書き方を提供しています

public class TryWithSample {
	@SuppressWarnings("unused")
	private static void read(File source) throws IOException {
	    try(InputStream fis =  new FileInputStream(source)) {

	    }
	    
	}
}


これによりJAVAはtryで申請したパイプを自動的に閉じます.