JDK7.0语法新特性

1,菱形语法(泛型实例化类型自动推断)
List<String> list = new ArrayList<>(); // <>这个真的很像菱形

2,在目前版本中,不可具体化的泛型(任意类型)可变参数,在编译时,会在调用处产生警告,JDK7里将这个警告挪到了方法定义处。
变化前:
static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
Callable<String> a, b, c;
...
// 警告处
return asList(a, b, c);
}

static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
Callable<String> a, b, c;
...
// 警告处
return asList(a, b, c);
}
变化后:
// 警告处
static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
Callable<String> a, b, c;
...
return asList(a, b, c);
}

// 警告处
static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
Callable<String> a, b, c;
...
return asList(a, b, c);
}

3,switch现在可以支持字符串了
String s = ...
switch(s) {
case "quux":
processQuux(s); //没有break,继续往下

case "foo":
case "bar":
processFooOrBar(s);
break;
case "baz":
processBaz(s); //没有break,继续往下

default:
processDefault(s);
break;
}

String s = ...
switch(s) {
case "quux":
processQuux(s); //没有break,继续往下

case "foo":
case "bar":
processFooOrBar(s);
break;
case "baz":
processBaz(s); //没有break,继续往下

default:
processDefault(s);
break;
}

4,支持二进制语法和单位级别的数字表示方式
// 8位byte
byte aByte = (byte)0b00100001;
// 16位short
short aShort = (short)0b1010000101000101;
// 32位int
int anInt1 = 0b10100001010001011010000101000101;

// 8位byte
byte aByte = (byte)0b00100001;
// 16位short
short aShort = (short)0b1010000101000101;
// 32位int
int anInt1 = 0b10100001010001011010000101000101;
支持单位级别的数字,提高可读性
long underScores = 9_223_372_036_854_775_807L; // 每三位加一下划线,等同于 9,223,372,036,854,775,807

long underScores = 9_223_372_036_854_775_807L; // 每三位加一下划线,等同于 9,223,372,036,854,775,807

5,从语法层面上支持集合,不再是数组的专利。
final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
final Set<Integer> primes = { 2, 7, 31, 127, 8191, 131071, 524287 };
final Map<Integer, String> platonicSolids = { 4 : "tetrahedron",
6 : "cube", 8 : "octahedron", 12 : "dodecahedron", 20 : "icosahedron"
};

final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
final Set<Integer> primes = { 2, 7, 31, 127, 8191, 131071, 524287 };
final Map<Integer, String> platonicSolids = { 4 : "tetrahedron",
6 : "cube", 8 : "octahedron", 12 : "dodecahedron", 20 : "icosahedron"
};

6,JSR 292 动态类型语言支持
Dynamic x = (动态语言脚本);
Object y = x.foo("ABC").bar(42).baz();

Dynamic x = (动态语言脚本);
Object y = x.foo("ABC").bar(42).baz();

7,动态资源管理
在目前版本的java中,当你操作流时,一定会加try..finally以保证出现异常时,流能被正确关闭。
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}

BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
在JDK7里,你只需要将资源定义在try()里,Java7就会在readLine抛异常时,自动关闭资源。另外,资源类必须实现 Disposable<?> 接口。支持管理多个资源

try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值