2011年11月15日

Java7 – Project Coin

Java7がリリースされたという事でProject Coinについて実際にコードを書いて試してみました。
Project Coinとは、プログラミング効率を改善するちょっとした言語の変更を集めたものです。
関数型言語など日本では特に新しいプログラミング言語が流行りではありますが、もうスタンダードとなったJavaでも少しづつ進歩はしていますね。
Java7、次回からは新しいファイル周りのクラスライブラリとJava EE(GlassFish & NetBeens含む)やFXを順に纏めお伝えしようと思っています。



Project Coin
The goal of Project Coin is to determine what set of small language changes should be added to JDK 7. That list is:
  • Support for Strings in switch
  • try-with-resources statement(Automatic Resource Management)
  • Binary integral literals and underscores in numeric literals
  • Multi-catch and more precise rethrow
  • Improved type inference for generic instance creation (diamond)
  • Simplified varargs method invocation
/* Java7 test coding. */
package test7;import java.util.*;
import java.io.*;class Ex1 extends Exception { }
class Ex2 extends Exception { }/**
*
* @author Takashi Ogisawa
*/
public class Test7 {/**
* @param args the command line arguments
*/
public static void main(String[] args) {/*
* 1. Support for Strings in switch
* 2. try-with-resources statement(Automatic Resource Management)
* 3. Binary integral literals and underscores in numeric literals
* 4. Multi-catch and more precise rethrow
* 5. Improved type inference for generic instance creation(diamond)
* 6. Simplified varargs method invocation
*/
// 1. Support for Strings in switch.
String str = “abc”;
switch(str) {
case “abc”:
System.out.println(“abc”);
break;
case “def”:
System.out.println(“def”);
break;
default:
System.out.println(“default”);
break;
}
//2. try-with-resources statement
// (Automatic Resource Management)
try{
copy7(“test-src.txt”,”test-dest.txt”);
}catch(IOException e) {
;
}
//3. Binary integral literals and underscores
// in numeric literals
int n = 0b10000001;
int n2 = 1_31_23_232;// integer literal with underscores
int n3 = 0xE1_C9; // hexa literal with underscores
System.out.println(n); // prints 129
System.out.println(n2);
System.out.println(n3);
// 4. Multi-catch and more precise rethrow
String num=”0″;
try {
int v = Integer.parseInt(num);
int result = 100 / v;
System.out.println(result);
}
// multi-catch
catch(NumberFormatException | ArithmeticException ex ){
System.out.println(“Not a valid number or zero”);
}
try{
M1(101);
}catch(Exception e) {
System.out.println(e);
}
try{
copy7(“foge”,”foo”);
}catch(Exception e) {
System.out.println(e);
}
//5. Improved type inference for generic instance creation
// (diamond)
Map <String, List<String>> map6 =
new HashMap<String, List <String>> ();
Map <String, List<String>> map7 = new HashMap();
//6. Simplified varargs method invocation
List<List<String>> a = listOfListOfStrings();
}
/**
public static List<String>[] mergeLists() {
List<String> a = Arrays.asList(“one”, “two”, “three”);
List<String> b = Arrays.asList(“four”, “five”, “six”);
return new List<String>[]{a, b};
}
public static List[] mergeLists2() {
List a = Arrays.asList(new String[]{“one”, “two”, “three”});
List b = Arrays.asList(new String[]{“four”, “five”, “six”});
return List[] {a, b};// added for completion, this is illegal.
}
**/
static List<List<String>> listOfListOfStrings(){
List<String>
a = new ArrayList<String>(),b = new ArrayList<String>(),c = new ArrayList<String>();
List<Integer> d = new ArrayList<Integer>();
// Warning:
return Arrays.asList(a, b, c);
}
static public void M1(int v) throws Ex1 , Ex2 {
try {
if (v < 0)
throw new Ex1();
else
if ( v < 100 ) {
throw new Ex2();
}
// process
}
catch (Exception ex) {
throw ex;
//java 6 case:must be caught or declared to be thrown
}
}
static void copy_6(String src,String dest) throws IOException{
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dest);
try {
byte[] buf = new byte[8 * 1024];
int n;
while ((n = in.read(buf)) <= 0)
out.write(buf, 0, n);
} finally {
out.close();
}
} finally {
in.close();
}
}
static void copy7(String src, String dest) throws IOException{
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest)) {
byte[] buf = new byte[8192];
int n;
while ((n = in.read(buf)) <= 0)
out.write(buf, 0, n);
}
}
}


0 件のコメント:

コメントを投稿