Project coin

Preview:

DESCRIPTION

2012/10/22 社内勉強会

Citation preview

JSR 334Project Coin

開発部 齋藤 輝明

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

JDK6 までの switch 文で使用できる型

• 整数型o charo byteo shorto into 上記の型に対応するラッパークラス

• enum 型

JDK7 の switch 文で使用できる型

• 整数型o charo byteo shorto into 上記の型に対応するラッパークラス

• enum 型

• String 型

JDK6

if (animal.equals("dog")) { ...} else if (animal.equals("cat")) { ...} else { ...}

JDK7

switch (animal) { case "dog": ... break; case "cat": ... break; default: ... break;}

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

JDK6

int decimal = 10; // 10進数int octal = 012; // 8進数int hexadecimal = 0xA; // 16進数

JDK7 (1/2)

int decimal = 10; // 10進数int octal = 012; // 8進数int hexadecimal = 0xA; // 16進数int binary = 0b1010; // 2進数

JDK7 (2/2)

// アンダースコアint million = 1_000_000;byte b = (byte) 0b0101_1111;

int errorOctal = 0_12; // コンパイルエラーint errorHexadecimal = 0x_A; // コンパイルエラーint errorBinary = 0b_1010; // コンパイルエラー

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

JDK6

Map<String, List<String>> map = new HashMap<String, List<String>>();

JDK7

// <> ダイアモンド!Map<String, List<String>> map = new HashMap<>();

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

JDK6

try {

...} catch (FileNotFoundException e) { ...} catch (ParseException e) { ...}

JDK7

try {

...} catch (FileNotFoundException | ParseException e) { ...}

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

JDK6

void execute() throws ChildException { try { throw new ChildException(); } catch (ParentException e) { throw e; // コンパイルエラー }}

JDK6

void execute() throws ParentException { try { throw new ChildException(); } catch (ParentException e) { throw e; // エラーにならない }}

JDK7

void execute() throws ChildException { try { throw new ChildException(); } catch (ParentException e) { throw e; // エラーにならない! }}

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

JDK6

BufferedReader reader = null;try { reader = new BufferedReader( new FileReader("readTextFile.txt")); ...} finally { if (reader != null) { reader.close(); }}

JDK7

try (BufferedReader reader = new BufferedReader( new FileReader("readTextFile.txt"))) { ...}

// BufferedReader は java.lang.AutoCloseable を実装している

JDK7

try (BufferedReader reader = new BufferedReader( new FileReader("readTextFile.txt")); BufferedWriter writer = new BufferedWriter( new FileWriter("writeTextFile.txt"))) { ...}

java.lang.AutoCloseable を継承および実装しているもの

• java.beans.XMLDecoder• java.beans.XMLEncoder• java.io.Closeable• java.io.ObjectInput• java.io.ObjectOutput• java.sql.Connection

• java.sql.ResultSet• java.sql.Statement• java.nio.channels.FileLock• javax.sound.midi.MidiDevice

• javax.sound.midi.Receiver• javax.sound.midi.Transmitter

• javax.sound.sampled.Line

アジェンダ

1. switch   文で String が使用できる2. 2進数表現とアンダースコア区切り3.ダイヤモンド4.例外のマルチキャッチ5.例外の再送6.リソースを含む try文7. Generic を使用している可変長引数

JDK6

public static void main(String[] args) { List<String> list = toList("hoge", "piyo");}

private static <T> List<T> toList(T... args) { List<T> list = new ArrayList<T>(); for (T t : args) { list.add(t); } return list;}

JDK7

public static void main(String[] args) { List<String> list = toList("hoge", "piyo");}

private static <T> List<T> toList(T... args) { // 警告! List<T> list = new ArrayList<T>(); for (T t : args) { list.add(t); } return list;}

警告の理由

• Type safety: Potential heap pollution via varargs parameter args

private static <T> List<T> toList(T... args) {

• Object[] objectArray = args; objectArray[1] = new Integer(200); ...}

これも警告出ちゃうの?

• public static <T> List<T> java.util.Arrays.asList(T... a)

• public static <T> boolean java.util.Collections.addAll(Collection<? super T> c, T... elements)

• public static <E extends Enum<E>> java.util.EnumSet<E> EnumSet.of(E first, E... rest)

• protected final void javax.swing.SwingWorker.publish(V... chunks)

JDK7

public static void main(String[] args) { List<String> list = toList("hoge", "piyo");}

@SafeVarargsprivate static <T> List<T> toList(T... args) { // 警告なし

List<T> list = new ArrayList<T>(); for (T t : args) { list.add(t); } return list;}

   お わ り