首页 > 编程语言 > 详细

Java7新特性(一)Coin

时间:2015-03-07 02:04:18      阅读:448      评论:0      收藏:0      [点我收藏+]

from:http://blog.csdn.net/tanxiang21/article/details/16859781
1.语法糖 数字下划线

 1  2  3  4  5  6  7  8  9
package com.java7developer.chapter1;
import java.util.Collection;
import java.util.HashMap;
public class Coin {
int test = 123_567;
long test1 = 100_000L;
}
 来自CODE的代码片
Coin.java

2.switch语句中的String

 1  2  3  4
public void printDay(String dayOfWeek){
case "Sunday":System.out.println("ddd");break;
default:System.out.println("sss");break;
}
 来自CODE的代码片
snippet_file_0.txt

3.multicatch

  1   2   3   4   5   6   7   8   9  10  11  12  13  14
public Configuration getConfig(String fileName) {
Configuration cfg = null;
try {
String fileText = getFile(fileName);
cfg = verifyConfig(parseConfig(fileText));
} catch (FileNotFoundException | ParseException | ConfigurationException e) {
System.err.println("Config file ‘" + fileName
+ "‘ is missing or malformed");
} catch (IOException iox) {
System.err.println("Error while processing file ‘" + fileName + "‘");
}
return cfg;
}
 来自CODE的代码片
snippet_file_0.txt

4.final重抛

对比上份代码
 1  2  3  4  5  6
try {
String fileText = getFile(fileName);
cfg = verifyConfig(parseConfig(fileText));
} catch (final Exception e) {
throw e;
}
 来自CODE的代码片
snippet_file_0.txt

5.try-with-resources(TWR) AutoCloseable

  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
package com.java7developer.chapter1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Java7ResourcesExample {
private void run() throws IOException {
File file = new File("foo");
URL url = null;
try {
url = new URL("http://www.google.com/");
} catch (MalformedURLException e) {
}
try (OutputStream out = new FileOutputStream(file);
InputStream is = url.openStream()) {
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
public static void main(String[] args) throws IOException {
Java7ResourcesExample instance = new Java7ResourcesExample();
instance.run();
}
}
 来自CODE的代码片
Java7ResourcesExample.java

6.钻石语法

 1
HashMap<String, String> a = new HashMap<>();
 来自CODE的代码片
Java7-新特性-钻石语法

7.变参 消失的警告 @SafeVarargs

  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
public class Coin {
int test = 123_567;
long test1 = 100_000L;
@SafeVarargs
public static <T> Collection<T> doSomething(T... entries){
return null;
}
public static void main(String[] args) {
HashMap<String, String> a = new HashMap<>();
HashMap<String, String> b = new HashMap<>();
doSomething(a,b);
}
}

Java7新特性(一)Coin

原文:http://www.blogjava.net/xiaomage234/archive/2015/03/06/423244.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!