InterviewSolution
Saved Bookmarks
| 1. |
Java 9 Inner Class Diamond Operator |
|
Answer» The PURPOSE of the diamond operator was to increase reusability of code, avoid redundant code which was ACHIEVED by leaving the generic type on the right SIDE of the expression. There was a certain problem with the diamond operator as it couldn’t be used with anonymous inner classes. Java 9 enhanced the diamond operator so that it could be used with anonymous inner classes. Let us look at an example of diamond operator with anonymous inner class abstract class ABC<T> { abstract T dif(T t1, T t2); } public class Example { public static void main(String[] args) { MyClass<Integer> obj = new MyClass<>() { Integer dif(Integer a, Integer b) { return a-b; } }; Integer dif = obj.dif(10,90); System.out.println(dif); } }The output is as FOLLOWS: $javac Example.java $java Example -80 |
|