Java7FeatureTest.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import java.io.DataOutputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.TreeMap;
  7. public class Java7FeatureTest {
  8. int thousand = 1_000;
  9. int million = 1_000_000;
  10. int foo = 1234_5678;
  11. long l = 1_2_3_4__5_6_7_8L;
  12. int binary = 0b0001_0010_0100_1000;
  13. int mask = 0b1010_1010_1010;
  14. long big = 9_223_783_036_967_937L;
  15. long creditCardNumber = 1234_5678_9012_3456L;
  16. long socialSecurityNumber = 999_99_9999L;
  17. float pi = 3.14_15F;
  18. long hexBytes = 0xFF_EC_DE_5E;
  19. long hexWords = 0xCAFE_BFFE;
  20. public static void diamondOperator() {
  21. // old
  22. Map<String, List<Object>> tradesOld = new TreeMap<String, List<Object>>();
  23. // new
  24. Map<String, List<Object>> trades = new TreeMap<>();
  25. }
  26. private void stringSwitch(String foo) {
  27. // old
  28. if (foo.equalsIgnoreCase("new")) {
  29. } else if (foo.equalsIgnoreCase("exec")) {
  30. } else if (foo.equalsIgnoreCase("pend")) {
  31. }
  32. // new
  33. switch (foo) {
  34. case "new":
  35. break;
  36. case "exec":
  37. break;
  38. case "pend":
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. public void oldTry() {
  45. FileOutputStream fos = null;
  46. DataOutputStream dos = null;
  47. try {
  48. fos = new FileOutputStream("movies.txt");
  49. dos = new DataOutputStream(fos);
  50. dos.writeUTF("Java 7 Block Buster");
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. } finally {
  54. try {
  55. fos.close();
  56. dos.close();
  57. } catch (IOException e) {
  58. }
  59. }
  60. }
  61. public void newTry() {
  62. try (FileOutputStream fos = new FileOutputStream("movies.txt"); DataOutputStream dos = new DataOutputStream(fos)) {
  63. dos.writeUTF("Java 7 Block Buster");
  64. } catch (IOException e) {
  65. }
  66. }
  67. public void oldMultiCatch() {
  68. try {
  69. methodThatThrowsThreeExceptions();
  70. } catch (ExceptionOne e) {
  71. } catch (ExceptionTwo e) {
  72. } catch (ExceptionThree e) {
  73. }
  74. }
  75. public void newMultiCatch() {
  76. try {
  77. methodThatThrowsThreeExceptions();
  78. } catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {
  79. }
  80. }
  81. public void newMultiMultiCatch() {
  82. try {
  83. methodThatThrowsThreeExceptions();
  84. } catch (ExceptionOne e) {
  85. } catch (ExceptionTwo | ExceptionThree e) {
  86. }
  87. }
  88. private void methodThatThrowsThreeExceptions() throws ExceptionOne, ExceptionTwo, ExceptionThree {
  89. }
  90. public void foo(String bar) throws ExceptionOne, ExceptionTwo {
  91. try {
  92. } catch (Exception e) {
  93. throw e;
  94. }
  95. }
  96. }