java7新特性

  1. 1,菱形语法(泛型实例化类型自动推断)
  2. List<String> list = new ArrayList<>(); // <>这个真的很像菱形
  3. 2,在目前版本中,不可具体化的泛型(任意类型)可变参数,在编译时,会在调用处产生警告,JDK7里将这个警告挪到了方法定义处。
  4. 变化前:
  5. static <T> List<T> asList(T... elements) { ... }
  6. static List<Callable<String>> stringFactories() {
  7. Callable<String> a, b, c;
  8. ...
  9. // 警告处
  10. return asList(a, b, c);
  11. }
  12. static <T> List<T> asList(T... elements) { ... }
  13. static List<Callable<String>> stringFactories() {
  14. Callable<String> a, b, c;
  15. ...
  16. // 警告处
  17. return asList(a, b, c);
  18. }
  19. 变化后:
  20. // 警告处
  21. static <T> List<T> asList(T... elements) { ... }
  22. static List<Callable<String>> stringFactories() {
  23. Callable<String> a, b, c;
  24. ...
  25. return asList(a, b, c);
  26. }
  27. // 警告处
  28. static <T> List<T> asList(T... elements) { ... }
  29. static List<Callable<String>> stringFactories() {
  30. Callable<String> a, b, c;
  31. ...
  32. return asList(a, b, c);
  33. }
  34. 3switch现在可以支持字符串了
  35. String s = ...
  36. switch(s) {
  37. case "quux":
  38. processQuux(s); //没有break,继续往下
  39. case "foo":
  40. case "bar":
  41. processFooOrBar(s);
  42. break;
  43. case "baz":
  44. processBaz(s); //没有break,继续往下
  45. default:
  46. processDefault(s);
  47. break;
  48. }
  49. String s = ...
  50. switch(s) {
  51. case "quux":
  52. processQuux(s); //没有break,继续往下
  53. case "foo":
  54. case "bar":
  55. processFooOrBar(s);
  56. break;
  57. case "baz":
  58. processBaz(s); //没有break,继续往下
  59. default:
  60. processDefault(s);
  61. break;
  62. }
  63. 4,支持二进制语法和单位级别的数字表示方式
  64. // 8位byte
  65. byte aByte = (byte)0b00100001;
  66. // 16位short
  67. short aShort = (short)0b1010000101000101;
  68. // 32位int
  69. int anInt1 = 0b10100001010001011010000101000101;
  70. // 8位byte
  71. byte aByte = (byte)0b00100001;
  72. // 16位short
  73. short aShort = (short)0b1010000101000101;
  74. // 32位int
  75. int anInt1 = 0b10100001010001011010000101000101;
  76. 支持单位级别的数字,提高可读性
  77. long underScores = 9_223_372_036_854_775_807L; // 每三位加一下划线,等同于 9,223,372,036,854,775,807
  78. long underScores = 9_223_372_036_854_775_807L; // 每三位加一下划线,等同于 9,223,372,036,854,775,807
  79. 5,从语法层面上支持集合,不再是数组的专利。
  80. final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
  81. final Set<Integer> primes = { 2, 7, 31, 127, 8191, 131071, 524287 };
  82. final Map<Integer, String> platonicSolids = { 4 : "tetrahedron",
  83. 6 : "cube", 8 : "octahedron", 12 : "dodecahedron", 20 : "icosahedron"
  84. };
  85. final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
  86. final Set<Integer> primes = { 2, 7, 31, 127, 8191, 131071, 524287 };
  87. final Map<Integer, String> platonicSolids = { 4 : "tetrahedron",
  88. 6 : "cube", 8 : "octahedron", 12 : "dodecahedron", 20 : "icosahedron"
  89. };
  90. 6,JSR 292 动态类型语言支持
  91. Dynamic x = (动态语言脚本);
  92. Object y = x.foo("ABC").bar(42).baz();
  93. Dynamic x = (动态语言脚本);
  94. Object y = x.foo("ABC").bar(42).baz();
  95. 7,动态资源管理
  96. 在目前版本的java中,当你操作流时,一定会加try..finally以保证出现异常时,流能被正确关闭。
  97. BufferedReader br = new BufferedReader(new FileReader(path));
  98. try {
  99. return br.readLine();
  100. } finally {
  101. br.close();
  102. }
  103. BufferedReader br = new BufferedReader(new FileReader(path));
  104. try {
  105. return br.readLine();
  106. } finally {
  107. br.close();
  108. }
  109. 在JDK7里,你只需要将资源定义在try()里,Java7就会在readLine抛异常时,自动关闭资源。另外,资源类必须实现 Disposable<?> 接口。支持管理多个资源
  110. try (BufferedReader br = new BufferedReader(new FileReader(path)) {
  111. return br.readLine();
  112. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值