本文的主要来源是LYNDA.COM.JAVA.ADVANCED.TRAINING,前面我会沿着video的内容来,后续我可能会加入一些别的内容。
这一小节我们看看Java 7的一些新特性(之前由于Android开发和J2EE开发大部分都是使用Java 5/6,直到今天才有机会学习啊)。
在switch...case语句中使用String,貌似这个是从微软学来的:
package com.freesoft.java7newfeature; public class TestStringSwitch { /** * @param args */ public static void main(String[] args) { final String KEY = "my key"; String mykey = "my key"; switch (mykey) { case KEY: System.out.println("mykey equals KEY."); break; default: System.out.println("mykey NOT equals KEY."); break; } } }
package com.freesoft.java7newfeature; import java.util.ArrayList; import com.freesoft.testentity.Olive; public class TestSimpleUseOfGenerics { /** * @param args */ public static void main(String[] args) { Olive o1 = new Olive("zhangsan", 0x0000001); Olive o2 = new Olive("lisi", 0x0000002); Olive o3 = new Olive("wangwu", 0x0000003); Olive[] oa = new Olive[]{o1, o2, o3}; System.out.println(oa); System.out.println("=================================="); ArrayList<Olive> ol = new ArrayList<>(); ol.add(o1); ol.add(o2); ol.add(o3); System.out.println(ol); } }
package com.freesoft.java7newfeature; import java.text.NumberFormat; public class TestNumberLiterals { public static void main(String[] args) { int i = 1_000_000_000; int j = 1_0000_0000; NumberFormat nf = NumberFormat.getInstance(); System.out.println(nf.format(i)); System.out.println(nf.format(j)); } }
package com.freesoft.java7newfeature; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; public class TestTryWithResource { public static void main(String[] args) { // ********************************************************************************************************************** // FileInputStream fs = null; // try { // fs = new FileInputStream(new File("test.dat")); // // fs dosomething()..... // } catch (FileNotFoundException e) { // e.printStackTrace(); // } finally{ // try { // fs.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } try (FileInputStream fs = new FileInputStream(new File("test.dat"))) { // TODO do something } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }