Friday 14 August 2015

Anonymous classes vs Lambda expressions

Anonymous classes

Anonymous classes are used to declare and instantiate a class at the same time. Anonymous are just like local classes, only difference is they don't have name.
public interface Operation {
    int operation(int var1, int var2);
}

public class OperationTest {
 public static void main(String args[]) {
  Operation add = new Operation() {
   public int operation(int a, int b) {
    return (a + b);
   }
  };

  Operation subtract = new Operation() {
   public int operation(int a, int b) {
    return (a - b);
   }
  };

  Operation multiply = new Operation() {
   public int operation(int a, int b) {
    return (a * b);
   }
  };

  Operation divide = new Operation() {
   public int operation(int a, int b) {
    return (a / b);
   }
  };

  Operation remainder = new Operation() {
   public int operation(int a, int b) {
    return (a % b);
   }
  };

  System.out.println("Sum of 11 and 5 is " + add.operation(11, 5));
  System.out.println("Subtraction of 11 and 5 is "
    + subtract.operation(11, 5));
  System.out.println("Multiplication of 11 and 5 is "
    + multiply.operation(11, 5));
  System.out
    .println("Division of 11 and 5 is " + divide.operation(11, 5));
  System.out.println("Remainder of 11 and 5 is "
    + remainder.operation(11, 5));

 }
}


Output
Sum of 11 and 5 is 16
Subtraction of 11 and 5 is 6
Multiplication of 11 and 5 is 55
Division of 11 and 5 is 2
Remainder of 11 and 5 is 1

Lambda expressions
Lambda expressions are new feature added in Java8. These are used to represent anonymous classes in compact format. Lambda expressions are applicable to functional interfaces only.


Same OperationTest class can be rewritten using lambda expressions like below.
public class OperationTest {
 public static void main(String args[]) {
  Operation add = (int a, int b) -> {
   return (a + b);
  };
  Operation subtract = (int a, int b) -> {
   return (a - b);
  };
  Operation multiply = (int a, int b) -> {
   return (a * b);
  };
  Operation divide = (int a, int b) -> {
   return (a / b);
  };
  Operation remainder = (int a, int b) -> {
   return (a % b);
  };

  System.out.println("Sum of 11 and 5 is " + add.operation(11, 5));
  System.out.println("Subtraction of 11 and 5 is "
    + subtract.operation(11, 5));
  System.out.println("Multiplication of 11 and 5 is "
    + multiply.operation(11, 5));
  System.out
    .println("Division of 11 and 5 is " + divide.operation(11, 5));
  System.out.println("Remainder of 11 and 5 is "
    + remainder.operation(11, 5));

 }
}


Output

Sum of 11 and 5 is 16
Subtraction of 11 and 5 is 6
Multiplication of 11 and 5 is 55
Division of 11 and 5 is 2
Remainder of 11 and 5 is 1


As you observe anonymous class

Operation add = new Operation() {
         public int operation(int a, int b) {
                  return (a + b);
         }
};

is replaced with

Operation add = (int a, int b) -> {return (a+b);};

Differences between Anonymous class and lambda expressions
1.   You can create anonymous class to any interface, where as you can create lambda expression to functional interface only.
2.   Inside an anonymous class, this refers to the anonymous class itself, but inside a lambda it refers to the enclosing class.
3.   Anonymous classes are used to shadow variables of the enclosing class, where as lambda expressions can't.

For example, when you tried to compile following program, you will get compiler error.

public class OperationTest {
 public static void main(String args[]) {
  int x = 10;
  
  Operation add = (int a, int b) -> {
   int x = 20;
   return (a + b);
  };
  
 }
}


$ javac OperationTest.java
OperationTest.java:7: error: variable x is already defined in method main(String[])
                       int x = 20;
                           ^
1 error

But following program with anonymous class works fine.

public class OperationTest {
 public static void main(String args[]) {
  int x = 10;

  Operation add = new Operation() {
   int x = 30;

   @Override
   public int operation(int a, int b) {
    int x = 10;
    return (a + b);
   }

  };

 }
}



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment