Menu

New features in java language in Java-9 or JDK-9 release

The major change in Java SE 9 is its module system. Other than this there are several small language changes as well in Java SE 9.

As we already know on 21st Sept 2017 JDK 9 or Java 9 has been release in market. Today we will see what all new programming features introduced in Java SE-9.


  • You can now declare a private method in a Interface class. This allows non-abstract methods of an interface to share code between them. 
For Example: 
public interface AccountOp{
public static final String NAME="wellDo";

//default method of interface
public default void foot(){}
//private method within interface
private void displayAge();
}


  • Underscore character no more legal for identifier name. Your source code will not compile if you use underscore character ["_"] in an identifier name[variables, methods, classes, packages and interfaces]. e.g. String ras_name; interface Local_Home; public void gender_Test(); are the illegal in Java 9/JDK 9.


  • Allow effectively final variables to be used as resources in the try-with-resources statement. If you already have a resource as a final or effectively final variable, you can use that variable in a try-with-resources statement without declaring a new variable. An "effectively final" variable is one whose value is never changed after it is initialized.
    For example, you declared these two resources:
              // A final resource

              final Resource resource1 = new Resource("resource1");
              // An effectively final resource
              Resource resource2 = new Resource("resource2");
              In Java SE 7 or 8, you would declare new variables, like this:
                        try (Resource r1 = resource1;

                             Resource r2 = resource2) {
                            ...
                        }
                        In Java SE 9, you don’t need to declare r1 and r2:
                          // New and improved try-with-resources statement in Java SE 9

                                  try (resource1;
                                       resource2) {
                                      ...
                                  }


                          • @SafeVarargs annotation is allowed on private instance methods.

                          The @SafeVarargs annotation can be applied only to methods that cannot be overridden. These include static methods, final instance methods, and, new in Java SE 9, private instance methods.


                          • You can use diamond syntax in conjunction with anonymous inner classes.
                          Types that can be written in a Java program, such as int or String, are called denotable types. The compiler-internal types that cannot be written in a Java program are called non-denotable types.
                          Non-denotable types can occur as the result of the inference used by the diamond operator. Because the inferred type using diamond with an anonymous class constructor could be outside of the set of types supported by the signature attribute in class files, using the diamond with anonymous classes was not allowed in Java SE 7.
                          In Java SE 9, as long as the inferred type is denotable, you can use the diamond operator when you create an anonymous inner class.

                          For more detail information please refer the Java official doc.


                          Reference:

                          1. https://docs.oracle.com/javase/9/language/toc.htm
                          2. http://www.oracle.com/technetwork/java/javase/9all-relnotes-3704433.html
                          3. https://docs.oracle.com/javase/9/whatsnew/toc.htm

                          No comments:

                          Post a Comment