What's new in Java7



Java Platform Standard Edition 7 is the newest release. There many new features added in this version. Please see few of the features and enhancements of Java 7 mentioned below:

        Strings in switch statements:   Now you can use String in Switch Case statement. It uses String.equals() method to get the correct case.
                        String day="Thursday";
                        switch (day){
                                    case "Monday":break;
                                    case "Tuesday": break;
                                    default:throw new Exception();         
                        }

        Catching Multiple Exception Types: There are a couple of improvements in the exception handling area. Java 7 introduced multi-catch functionality to catch multiple exception types using a single catch block.Please see the code below to understand the multi catching in a better way:
                        public void newMultiCatch() {
                                    try {
                                                      throwThreeExceptions();
                                        } catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {
                                                     // log and deal with all Exceptions
                                           }
                                }
        Type Inference for Generic Instance Creation:  Earlier if we have to declare a generics statemenr we had to write the below:
                        Map> trades = new TreeMap> ();
                        But that is kind of redundant putting the same declaration on both the side, but in Java    7 we don't have to type the whole list of types for instantiation. Instead use the <>     symbol , which is called the diamond operator. The above statement will look like            below:
                        Map> trades = new TreeMap <> ();

        Automatic resource management: Previously resources like Connections, Files, Input/OutStreams, etc. should be closed manually, however in Java 7 it manages all these resources automatically. To do this you have to include these resources in the try statement so that it can close it automatically.Please find the example below where Buffered Reader will be closed automatically:
                        static String readFirstLineFromFile(String path) throws IOException {
                                    try (BufferedReader br = new BufferedReader(new FileReader(path))) {
                                      return br.readLine();
                                    }
                        }

        Numeric literals with underscores: It's quite error prone and cumbersome to identify a literal if it's a million or a billion unless you count the places from right to left.Java 7 introduced underscores in identifying the places. For example, you can declare 1000 as shown below:
                        int thousand =  1_000;
                        or 1000000 (one million) as follows
                        int million  =  1_000_000

0 comments: